ccf_201709-2_公共鑰匙盒(java&c++)

試題編號: 201709-2
試題名稱: 公共鑰匙盒
時間限制: 1.0s
內存限制: 256.0MB
問題描述:

問題描述

  有一個學校的老師共用N個教室,按照規定,所有的鑰匙都必須放在公共鑰匙盒裏,老師不能帶鑰匙回家。每次老師上課前,都從公共鑰匙盒裏找到自己上課的教室的鑰匙去開門,上完課後,再將鑰匙放回到鑰匙盒中。
  鑰匙盒一共有N個掛鉤,從左到右排成一排,用來掛N個教室的鑰匙。一串鑰匙沒有固定的懸掛位置,但鑰匙上有標識,所以老師們不會弄混鑰匙。
  每次取鑰匙的時候,老師們都會找到自己所需要的鑰匙將其取走,而不會移動其他鑰匙。每次還鑰匙的時候,還鑰匙的老師會找到最左邊的空的掛鉤,將鑰匙掛在這個掛鉤上。如果有多位老師還鑰匙,則他們按鑰匙編號從小到大的順序還。如果同一時刻既有老師還鑰匙又有老師取鑰匙,則老師們會先將鑰匙全還回去再取出。
  今天開始的時候鑰匙是按編號從小到大的順序放在鑰匙盒裏的。有K位老師要上課,給出每位老師所需要的鑰匙、開始上課的時間和上課的時長,假設下課時間就是還鑰匙時間,請問最終鑰匙盒裏面鑰匙的順序是怎樣的?

輸入格式

  輸入的第一行包含兩個整數NK
  接下來K行,每行三個整數wsc,分別表示一位老師要使用的鑰匙編號、開始上課的時間和上課的時長。可能有多位老師使用同一把鑰匙,但是老師使用鑰匙的時間不會重疊。
  保證輸入數據滿足輸入格式,你不用檢查數據合法性。

輸出格式

  輸出一行,包含N個整數,相鄰整數間用一個空格分隔,依次表示每個掛鉤上掛的鑰匙編號。

樣例輸入

5 2
4 3 3
2 2 7

樣例輸出

1 4 3 2 5

樣例說明

  第一位老師從時刻3開始使用4號教室的鑰匙,使用3單位時間,所以在時刻6還鑰匙。第二位老師從時刻2開始使用鑰匙,使用7單位時間,所以在時刻9還鑰匙。
  每個關鍵時刻後的鑰匙狀態如下(X表示空):
  時刻2後爲1X345;
  時刻3後爲1X3X5;
  時刻6後爲143X5;
  時刻9後爲14325。

樣例輸入

5 7
1 1 14
3 3 12
1 15 12
2 7 20
3 18 12
4 21 19
5 30 9

樣例輸出

1 2 3 5 4

評測用例規模與約定

  對於30%的評測用例,1 ≤ NK ≤ 10, 1 ≤ w ≤ N, 1 ≤ sc ≤ 30;
  對於60%的評測用例,1 ≤ NK ≤ 50,1 ≤ w ≤ N,1 ≤ s ≤ 300,1 ≤ c ≤ 50;
  對於所有評測用例,1 ≤ NK ≤ 1000,1 ≤ w ≤ N,1 ≤ s ≤ 10000,1 ≤ c ≤ 100。

 

java:(100分通過)

法一:只用二維數組排序

轉載自 https://blog.csdn.net/qq_26580757/article/details/78513540

其中:多維數組按某列排序可參看https://www.cnblogs.com/whaozl/p/4061993.html

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
 
public class Main {
	static int res[];	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int a[][] = new int[k][3];
		for (int i = 0; i < a.length; i++) {
			a[i][0] = sc.nextInt();
			a[i][1] = sc.nextInt();
			a[i][2] = sc.nextInt()+a[i][1];
		}
		int as[][] = a.clone();
		int ae[][] = a.clone();
		Sort(as,1);
		Sort(ae,2);	
		res = new int[n+1];
		for (int i = 1; i < res.length; i++) {
			res[i] = i;
		}	
		int t = 0;
		int s = 0,e = 0;
		while((t++)<10101){//10001不夠,只有70分;所以記得加上上課時間c,最長爲100;		
			for (; e < ae.length; e++) {
				if(t<ae[e][2]) break;
				if(t==ae[e][2]){
					PBack(ae[e][0]);
				}
			}
			for (; s < as.length; s++) {
				if(t<as[s][1]) break;
				if(t==as[s][1]){
					Use(as[s][0]);
				}
			}		
			if(s==as.length&&e==ae.length) break;
		}
		ShowAns();
	}
	private static void PBack(int name) {
		for (int i = 1; i < res.length; i++) {
			if(res[i] == -1){
				res[i] = name;
				break;
			}
		}
	}
	private static void Use(int name) {
		for (int i = 1; i < res.length; i++) {
			if(res[i] == name){
				res[i] = -1;
				break;
			}
		}
	}
	private static void Sort(int[][] a,int i) {
		Arrays.sort(a,new Comparator<int[]>(){
			public int compare(int[] o1, int[] o2) {
				if(o1[i]==o2[i]) return o1[0]-o2[0];
				return o1[i]-o2[i] ;
			}
		});
	}
	public static void ShowAns(){
		for (int i = 1; i < res.length; i++) {
			System.out.print(res[i]+" ");
		}
		System.out.println();
	}
}

法二:

轉載自:https://blog.csdn.net/hunjue0915/article/details/80670903

import java.util.*;

public class Main {
    static int time=1,maxtime=0,keynum;
    static int[] keylist;
    static List<Teacher> teacherList=new ArrayList<>();
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        keynum=in.nextInt();
        int teachernum=in.nextInt();
        for(int i=0;i<teachernum;i++){
            int key=in.nextInt();
            int s=in.nextInt();
            int c=in.nextInt();
            if (s+c>maxtime){
                maxtime=s+c;
            }
            Teacher teacher=new Teacher(key,s,s+c);
            teacherList.add(teacher);
        }
        keylist=new int[keynum];
        for (int i=0;i<keynum;i++){
            keylist[i]=i+1;
        }
        while (time<=maxtime){
            returnkey();
            getkey();
            time++;
        }
        for (int i=0;i<keylist.length;i++){
            System.out.print(keylist[i]+" ");
        }
    }
 
    private static void getkey() {
        for (int i=0;i<teacherList.size();i++){
            if (teacherList.get(i).s==time){
                int temp=teacherList.get(i).key;
                for (int j=0;j<keylist.length;j++){
                    if (keylist[j]==temp){
                        keylist[j]=0;
                    }
                }
            }
        }
    }
 
    private static void returnkey() {
        List<Integer> returnlist=new ArrayList<>();
        for (int i=0;i<teacherList.size();i++){
            int temp=teacherList.get(i).end;
            if (temp==time){
                returnlist.add(teacherList.get(i).key);
            }
        }
        if (returnlist.isEmpty()){
            return;
        }else {
            //將要歸還的鑰匙從小到大排序
            for (int i = 0; i < returnlist.size() - 1; i++) {
                for (int j = 0; j < returnlist.size() - 1 - i; j++) {
                    if (returnlist.get(j) > returnlist.get(j + 1)) {
                        int temp = returnlist.get(j);
                        returnlist.set(j, returnlist.get(j + 1));
                        returnlist.set(j + 1, temp);
                    }
                }
            }
        }
        int m=0;
 
        for (int i=0;i<keylist.length;i++){
            if (keylist[i]==0){
                keylist[i]=returnlist.get(m);
                if (m<returnlist.size()-1){
                    m++;
                }else {
                    break;
                }
            }
        }
    }
 
    static class Teacher{
        int key,s,end;
        public Teacher(int key, int s, int end) {
            this.key = key;
            this.s = s;
            this.end = end;
        }
    }
}

c++:

#include<bits/stdc++.h>

using namespace std;

//發生事件的結構體
class node{
public:
    int bt;//發生的時間
    int key;//鑰匙編號
    int act;//借或者換,表示爲1和0
};

//排序函數,按時間、動作、鑰匙編號升序排列
bool cmpnode(const node &a,const node &b){
    if(a.bt!=b.bt) return a.bt<b.bt;
    if(a.act!=b.act) return a.act<b.act;
    return a.key<b.key;
}

int main(){
    int n,k; cin>>n>>k;
    
    //  產生鑰匙盒與事件數組
    int *keys=new int[n];
    node *acts=new node[2*k];

//  初始化鑰匙盒數組
    for(int i=0;i<n;i++)
        keys[i]=i+1;

//  讀取上下課時間,拆分爲取和換鑰匙事件
    for(int i=0;i<k;i++){
        int w, s, c;cin>>w>>s>>c;
        acts[2*i].key=w;
        acts[2*i].bt=s;
        acts[2*i].act=1;
        acts[2*i+1].key=w;
        acts[2*i+1].bt=s+c;
        acts[2*i+1].act=0;
    }

//  多條件排序(時間、動作、鑰匙編號)
    sort(acts,acts+2*k,cmpnode);

    for(int i=0;i<2*k;i++){
        if(acts[i].act==1){//取鑰匙 key->0
            for(int j=0;j<n;j++){
                if(keys[j]==acts[i].key){
                    keys[j]=0;
                    break;
                }
            }
        }
        else{ //還鑰匙 0->key
            for(int j=0;j<n;j++){
                if(keys[j]==0){
                    keys[j]=acts[i].key;
                    break;
                }
            }
        }
    }

//  打印輸出
    for(int i=0;i<n;i++){
        cout<<keys[i];
        if(i!=n-1) cout<<" ";//特殊處理最後一個鑰匙
    }

//  刪除動態數組
    delete []acts;
    delete []keys;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章