PAT 1026. Table Tennis

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players' info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2

分析
这道题是一道比较复杂的模拟题,并且题意也比较模糊,下面就列出几点需要注意的坑点;
1.当vip来了,如果有vip桌子和普通桌子都是空着的,他会优先使用编号较小的vip桌子,毕竟人家的vip身份在那摆着嘛。
2.在最后输出等待的时间时单位是分钟,且四舍五入。
3.当有人的打球的时间超过120分钟时,只看作是120,剩下的不要。
4.可以有人在08:00:00之前来,但要等到08:00:00。
深深的体会到有错却不得寻的痛苦,下面附上几组测试数据
//边缘测试例子
第一组测试数据
1
21:00:00 10 1
3 3
1 2 3
答案
0 0 0
第二组测试数据
1
20:59:59 10 1
3 3
1 2 3
答案
20:59:59 20:59:59 0
1 0 0
第三组测试数据
0
3 1
2
答案
0 0 0
//vip桌子分配例子,有vip桌子时,优先把vip分到编号小的vip桌子,而不是编号小的vip桌子
4
06:00:00 30 1
08:00:00 30 1
10:00:00 30 1
12:00:00 30 1
5 1
3
答案为:
06:00:00 08:00:00 120
08:00:00 08:00:00 0
10:00:00 10:00:00 0
12:00:00 12:00:00 0
1 0 3 0 0

//超过两小时的例子
2
18:00:00 180 1
20:00:00 60 1
1 1
1
答案为:
18:00:00 18:00:00 0
20:00:00 20:00:00 0
2

//关于四舍五入为1分钟的例子,大约等于30秒才+1分钟,小于30则不+
3
07:59:31 60 1
07:59:29 60 1
08:00:30 100 1
2 1
1
答案为:
1
07:59:29 08:00:00 1
07:59:31 08:00:00 0
08:00:30 09:00:00 60
2 1

至于代码嘛,我就不详细说了

#include<iostream>
#include<vector>
#include<algorithm> 
using namespace std;
struct student{
/*atime(arrive_time),stime(serve_time),ptime(play_time),wtime(wait_time)*/
	int atime,stime,ptime,wtime,vip; 
};
struct table{
/*usetime(该桌子下次最早使用时间),serve_no(服务次数)*/
	int usetime=8*60*60,vip=0,serve_no=0;
};
bool cmp(const student& s1,const student& s2){
	     return s1.atime<s2.atime;
}
bool comp(const table& t1,const table& t2){
	     return t1.usetime<t2.usetime;
}
int main(){
	int n,hour,min,sec,k,m,no;
	cin>>n;
	vector<student> q; 
	for(int i=0;i<n;i++){
		student stu;
		scanf("%d:%d:%d %d %d",&hour,&min,&sec,&stu.ptime,&stu.vip);
		stu.atime=hour*60*60+min*60+sec;
		/*截取有效play_time*/
		stu.ptime=stu.ptime>120?120:stu.ptime;
		q.push_back(stu);
	}
	/把人们按到达时间从早到晚排序*/
	sort(q.begin(),q.end(),cmp);
	cin>>k>>m; 
	vector<table> tables(k);
	/*录入各个桌子是否是vip桌子*/
	for(int i=0;i<m;i++){
		cin>>no;
		tables[no-1].vip=1;
	}
	while(1){
	/*迭代器it指向tables中可以使用时间最早的那个*/
		auto it=min_element(tables.begin(),tables.end(),comp);
	/*判断是否终止*/
		if(it->usetime>=21*3600||q.size()==0||q[0].atime>=21*3600) break;
	/*迭代器first_vip指向队列中第一个为vip的*/
		auto first_vip=find_if(q.begin(),q.end(),[](const student& s){return s.vip==1;});
		/*下面的处理队首为vip成员时(选取最小号vip桌子的情况)*/
		if(q[0].vip==1){
	       auto itt=find_if(tables.begin(),tables.end(),[q](const table&t){
	       if(t.vip==1&&t.usetime<=q[0].atime) return true; else return false;});
	       if(itt!=tables.end()){
	       	  itt->usetime=q[0].atime+q[0].ptime*60;
	       	  itt->serve_no++;
	       	  printf("%02d:%02d:%02d %02d:%02d:%02d %d
",q[0].atime/3600,q[0].atime%3600/60,q[0].atime%3600%60,
			          q[0].atime/3600,q[0].atime%3600/60,q[0].atime%3600%60,0);
			  q.erase(q.begin());
			  continue;
		   } 
		}
 /*下面是vip桌子空了出来,如果此时已经有vip成员到了,则该桌子属于他*/
		if(it->vip==1&&first_vip!=q.end()&&first_vip->atime<=it->usetime){                  
			   it->serve_no++;
			   printf("%02d:%02d:%02d %02d:%02d:%02d %d
",first_vip->atime/3600,first_vip->atime%3600/60,first_vip->atime%3600%60,
			          it->usetime/3600,it->usetime%3600/60,it->usetime%3600%60,(int)((it->usetime-first_vip->atime)/60.0+0.5));
			   it->usetime+=first_vip->ptime*60;
			   q.erase(first_vip);
		}
	/*接下来就普通的情况,队首来接玩空出来的桌子*/
		else{
		/*在桌子空出后到达,不要等待*/
				if(q[0].atime>it->usetime){
					it->usetime=q[0].atime+q[0].ptime*60;
				    printf("%02d:%02d:%02d %02d:%02d:%02d %d
",q[0].atime/3600,q[0].atime%3600/60,q[0].atime%3600%60,
			          q[0].atime/3600,q[0].atime%3600/60,q[0].atime%3600%60,0);
				}
		/*在桌子空出来前到达,需等待*/
				else{
					printf("%02d:%02d:%02d %02d:%02d:%02d %d
",q[0].atime/3600,q[0].atime%3600/60,q[0].atime%3600%60,
			          it->usetime/3600,it->usetime%3600/60,it->usetime%3600%60,(int)((it->usetime-q[0].atime)/60.0+0.5));
			        it->usetime+=q[0].ptime*60;  
				}
				it->serve_no++;
				q.erase(q.begin());
			}                                                           	
	}
	/*输出每个桌子服务人次*/
	for(int i=0;i<k;i++)
	i>0?cout<<" "<<tables[i].serve_no:cout<<tables[i].serve_no;
	return 0;
}
原文地址:https://www.cnblogs.com/A-Little-Nut/p/8289096.html