CSP certified public key box (C ++)

Problem Description

Question number: 201709-2
Questions Name: Public key box
time limit: 1.0s
Memory Limit: 256.0MB
Problem Description:

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 NK .
  The next K lines of three integers wSc , 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

  对于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。

解题思路:(读题之后,观察数据范围,判断可以使用双层循环,故不需要特别关注时间复杂度)

首先构造一个Teacher类,记录某位教师使用钥匙的开始时间、结束时间以及钥匙编号;

再设一个数组key[n+1](位置 0 不用),记录位置 i 处放置的钥匙编号key[i];

由于存在同时还钥匙的情况,且还钥匙时需要按编号从小到大还,故设一个bool型数组blkey[n+1],记录当前需要归还的钥匙有哪些(false表示不需要归还,true表示需要归还);

然后设一个记录最大结束时间的变量max,控制时间的结束;

最后设置一个time变量开始判断当前时间是上课开始时间还是结束时间,正常模拟......

不过需要注意①第 i 号钥匙归还之后,blkey[i]要重置为false;②一定要先还钥匙再借钥匙。

实现代码如下:

#include<bits/stdc++.h>
using namespace std;

struct Teacher{
	int start;
	int end;
	int roomID;
}; //  k名老师使用教师 
 
int main(){
	int n, k;
	scanf("%d %d",&n,&k);
	Teacher tea[k];
	int key[n+1] = {0};   // 第 i 个位置放置钥匙的编号 
	bool blkey[n+1] = {false};  // 标记该钥匙是否需要被归还,便于多枚钥匙同时归还时,从小到大还 
	for(int i = 1; i < n+1; i++){
		key[i] = i;  // 初始时,第 i 把钥匙放在位置 i 处 ( i > 0)
	} 
	int max = 0; 
	for(int i = 0; i < k; i++){
		int temp;
		scanf("%d %d %d",&tea[i].roomID,&tea[i].start,&temp);
		tea[i].end = tea[i].start + temp;
		if(tea[i].end > max) max = tea[i].end;  // 最晚的结束时间 
	}
	int time = 1;
	while(time <= max){  // 判断当前时间应该借钥匙/还钥匙 (若同时则先还后借) 
		for(int i = 0; i < k; i++){
			if(time == tea[i].end){ // 可能有多位老师同时要还钥匙 
				blkey[tea[i].roomID] = true;
			}
		}
		for(int i = 1; i < n+1; i++){  // 按从小到大的顺序归还钥匙 
			if(blkey[i]){  // 第 i号钥匙需要归还 
				for(int j = 1; j < n+1; j++){  // 判断是否有空位置 j
					if(key[j] == 0){  // 空位置 j  
						key[j] = i;  // 钥匙 i 放到位置 j 上 
						blkey[i] = false;   // 放置好钥匙 i之后,重置该钥匙不需要再还 
						break;
					} 
				}
			} 
		}
		for(int i = 0; i < k; i++){  
			if(time == tea[i].start){ // 第 i 位老师要借钥匙   
				for(int j = 1; j < n+1; j++){
					if(key[j] == tea[i].roomID){
						key[j] = 0;   // 借完后该位置置为 0  
						break;
					}	
				} 
			}
		}
		time++;
	}
	for(int i = 1; i < n+1; i++){
		printf("%d ",key[i]);
	}
	printf("\n"); 
}

 

发布了31 篇原创文章 · 获赞 2 · 访问量 1605

Guess you like

Origin blog.csdn.net/qq_38969094/article/details/104780318