CSP 201709-2 public key box (Java)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/l870358133/article/details/101264924

Problem Description

  There is a school teacher shared N classrooms, in accordance with the provisions of all the keys have to be placed in the public key box, the teacher can not take the keys home. Before each teacher in the class, they have to find their own public school classrooms from the key box key to open the door, after class, and then put the key back to the key box.
  A key box total N hooks, from left to right in a row, to hang N key classrooms. A bunch of keys hanging position is not fixed, but is labeled with a key, so teachers will not confuse the key.
  Every time you pick up the keys, and teachers will find the keys they need to be removed, while the other key does not move. Each also key when the teacher also will find the key hook leftmost empty, the key hanging on the hook. If there are many teachers also key, then press the key to their numbers in ascending order also. If the same time both the teachers are the key to another teacher to take the key, the first key teachers will also go back and remove the whole.
  Today is the beginning of the key in numerical order from small to large in the key box. There K teachers to attend classes, each teacher is given a key needed, start time and duration of the class class, assuming that the class time is also a key time, what order the keys inside the key box is ultimately what?

Input Format

  The first line of input contains two integers N , K .
  The next K lines of three integers w , S , c , represent the key number you want to use a teacher, school start time and duration of the class. There may be many teachers using the same key, but the teacher using a key time will not overlap.
  Ensure that the input data satisfies the input format, you do not have to check the legitimacy of data.

Output Format

  An output line, comprising N integers, separated by a space between adjacent integers, respectively for each of the hanging hook key number.

Sample input

5 2
4 3 3
2 2 7

Sample Output

1 4 3 2 5

Sample Description

  First teacher from the time to start using key 3 No. 4 classrooms, using three units of time, so at the moment 6 also key. Second teacher from the time 2 to start using the key, use 7 unit of time, so at the moment 9 also key.
  Key state after each critical time follows (X represents the empty):
  After a time 2 to 1X345;
  after 3 time 1X3X5;
  after time 6 143X5;
  the time is 9 14325.

Sample input

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

Sample Output

1 2 3 5 4

Evaluation scale cases and agreed with

  For 30% of the evaluation use cases,. 1 ≤ N , K ≤ 10,. 1 ≤ WN ,. 1 ≤ S , C ≤ 30;
  for 60% of the evaluation use cases,. 1 ≤ N , K ≤ 50, 1 ≤ WN , ≤. 1 S ≤ 300, 1 ≤ C ≤ 50;
  for reviews all use cases,. 1 ≤ N , K ≤ 1000,1 ≤ WN ,. 1 ≤ S ≤ 10000,1 ≤ C ≤ 100.


import java.util.*;
public class Main {
	/*
	 * BorrowKey类存储取钥匙的相关信息
	 */
	static class BorrowKey implements Comparable<BorrowKey> {
		int keyNum;	//钥匙编号
		int start;	//取走时间
		int time;	//时常
		public BorrowKey(int keyNum, int start, int time) {
			this.keyNum = keyNum;
			this.start = start;
			this.time = time;
		}
		public int compareTo(BorrowKey o) {		//根据取走的先后时间顺序排序
			return this.start-o.start;
		}
		
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] Key = new int[N+1];
		for(int i=1;i<=N;i++) {
			Key[i] = i;
		}
		int[][] flag = new int[N+1][2];
		BorrowKey[] borrowKey = new BorrowKey[K];
		for(int i=0;i<K;i++) {
			borrowKey[i] = new BorrowKey(sc.nextInt(), sc.nextInt(), sc.nextInt());
		}
		Arrays.sort(borrowKey);		//排序
		int count1 = 0;				//count1用来确定当前是否有空的挂钩
		int count2 = 0;				//标记当前是第count2次取走钥匙  (count2<K)
		int maxTime = 0;
		for(int i=1;;i++) {
			//还钥匙部分
			if(count1 != 0) {
				for(int j=1;j<=N;j++) {
					if(i == flag[j][1]) {
						for(int k=1;k<=N;k++) {
							if(Key[k]==0) {
								Key[k] = j;
								count1--;
								flag[j][0] = 0;
								flag[j][1] = 0;
								break;
							}
						}
					}
				}
			}
			//借钥匙部分
			if(count2 < K) {
				BorrowKey tmp = borrowKey[count2];
				if(tmp.start == i) {
					for(int j=1;j<=N;j++) {
						if(tmp.keyNum == Key[j]) {
							Key[j] = 0;
							count1++;
							flag[tmp.keyNum][0] = tmp.start;
							flag[tmp.keyNum][1] = tmp.start + tmp.time;
							if(flag[tmp.keyNum][1]>maxTime)
								maxTime = flag[tmp.keyNum][1];
							count2++;
							break;
						}
					}
				}
			}
			else {
				if(i>maxTime)
					break;
			}
		}
		for(int i=1;i<=N;i++)
			System.out.print(Key[i]+" ");
	}
}

This is a simulation title, teachers pick up the keys and simulate the whole process of the return of the keys, I might think the process was too complicated and did not expect any good solution, and finally run time overrun, if it is a formal examination may be slightly better a little, because the time limit given in the formal examination longer, if we have a good way to welcome more comments.

Guess you like

Origin blog.csdn.net/l870358133/article/details/101264924