题目链接:http://www.patest.cn/contests/pat-a-practise/1026
题目:
1026. Table Tennis (30)
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 2Sample 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
分析:
设置场景,VIP玩家,普通玩家,VIP桌子和普通桌子,根据到达时间的先后进行分配,相对于1014的银行排队的题目增加了VIP的情况:如果有V桌和V人,先安排他们,如果没有V桌了,则V人和普通玩家一样,按照到达时间先后来安排。
要写一个时间和秒转化的函数,可以写时间的循环,不过会有延时,所以这里增加了找出下一个判断时刻。
注意:
如果三个人的来到的时间不同,但是开始运动的时间相同,则先打印有VIP桌子和VIP的顾客,然后再是普通的,如果没有VIP桌,则按来到的时间进行升序打印。
针对很多桌子同时空出来的情况,先处理有V桌和V人的。
AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Time{
int hour;
int minute;
int sec;
};
int all2sec(Time t){//把时间格式转化为秒
return t.hour * 60 * 60 + t.minute * 60 + t.sec;
}
Time sec2all(int se){//把秒转化为时间格式
Time ttt;
ttt.hour = se / 3600;
ttt.minute = se / 60 % 60;
ttt.sec = se % 60;
return ttt;
}
struct Player{//每个玩家的结构体,包括到达时刻,开始玩的时刻,玩的时间,结束时刻
int s_time;
int start_time;
int service_time;
int end_time;
};
Player Normal[10001];//普通玩家
Player VIP[10001];//VIP玩家
int Table[101];//桌子上玩家的结束时间
bool is_V_table[101];//是否是VIP桌子
bool cmp(Player A, Player B){//玩家按照到达的时间排序
return A.s_time < B.s_time;
}
int num_of_player[101];//每张桌子招待的玩家
int main(void){
//freopen("F://Temp/input.txt", "r", stdin);
int N,nor_Idx,vip_Idx;
while (scanf("%d", &N) != EOF){
nor_Idx = 0; vip_Idx = 0;
for (int i = 0; i < N; i++){
int service, vv;
Time tt;
scanf("%d:%d:%d%d%d", &tt.hour, &tt.minute, &tt.sec,&service,&vv);
if (vv == 1){//如果是vip,则放入VIP的集合中
VIP[vip_Idx].s_time = all2sec(tt);
if (service > 120)service = 120;//每个玩家最多玩2个小时
VIP[vip_Idx].service_time = service * 60;
VIP[vip_Idx].end_time = 0;
vip_Idx++;//VIP[]的下标
}
else{//否则放入普通玩家集合中
Normal[nor_Idx].s_time = all2sec(tt);
Normal[nor_Idx].service_time = service * 60;
Normal[nor_Idx].end_time = 0;
nor_Idx++;
}
}//init
int m, v_num;
int vip_num = vip_Idx, nor_num = nor_Idx;
scanf("%d%d", &m, &v_num);//输入总桌子数和VIP桌子数
for (int i = 0; i < m; i++){
is_V_table[i] = false;
Table[i] = 0;
num_of_player[i] = 0;
} //init
for (int i = 0; i < v_num; i++){
int tmp;
scanf("%d", &tmp);
is_V_table[tmp - 1] = true;//标记VIP桌子
}
sort(Normal, Normal + nor_num, cmp);//普通玩家排序,按照到达的时间先后
sort(VIP, VIP + vip_num,cmp);//VIP玩家排序
nor_Idx = 0; vip_Idx = 0;
for (int ti = 28800; ti < 75600; ti++){//time_loop,按照时间排序,类似1014银行排队的那道题
for (int i = 0; i < m; i++){
if (Table[i] != 0){
if (ti == Table[i]){//如果桌子上有玩家并且玩家结束时间到了,那么桌子重新置为空闲
//num_of_player[i] ++;
Table[i] = 0;
}
}
}
Time t_reach, t_start;
for (int i = 0; i < m; i++){
for (int j = 0; j < m; j++){
if (is_V_table[j]){
if (Table[j] == 0){//如果是VIP的空桌子
if (vip_Idx < vip_num && VIP[vip_Idx].s_time <= ti){//如果有VIP玩家
VIP[vip_Idx].start_time = ti;//VIP玩家的开始时间等于当前时间
VIP[vip_Idx].end_time = ti + VIP[vip_Idx].service_time;//结束时间为当前时间加玩的时间
Table[j] = VIP[vip_Idx].end_time;//桌子结束时间是当前玩家的结束时间
//
t_reach = sec2all(VIP[vip_Idx].s_time);//转化成时间格式便于输出
t_start = sec2all(VIP[vip_Idx].start_time);
int cha = VIP[vip_Idx].start_time - VIP[vip_Idx].s_time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t_reach.hour, t_reach.minute, t_reach.sec, t_start.hour, t_start.minute, t_start.sec, int(cha * 1.0 / 60 + 0.5));
//
vip_Idx++;//下一位VIP玩家
num_of_player[j] ++;//下一个玩家
}
}
}
}
for (int j = 0; j < m; j++, i++){
if (Table[j] == 0){//普通空闲桌子
if (nor_Idx < nor_num && vip_Idx < vip_num && Normal[nor_Idx].s_time < VIP[vip_Idx].s_time && Normal[nor_Idx].s_time <= ti){//普通玩家比VIP玩家来的早,普通玩家占用普通桌子
Normal[nor_Idx].start_time = ti;
Normal[nor_Idx].end_time = ti + Normal[nor_Idx].service_time;
Table[j] = Normal[nor_Idx].end_time;
//
t_reach = sec2all(Normal[nor_Idx].s_time);
t_start = sec2all(Normal[nor_Idx].start_time);
int cha = Normal[nor_Idx].start_time - Normal[nor_Idx].s_time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t_reach.hour, t_reach.minute, t_reach.sec, t_start.hour, t_start.minute, t_start.sec, int(cha * 1.0 / 60 + 0.5));
//
nor_Idx++;
num_of_player[j] ++;
}
else if (nor_Idx < nor_num && vip_Idx < vip_num && Normal[nor_Idx].s_time > VIP[vip_Idx].s_time && VIP[vip_Idx].s_time <= ti){//VIP玩家比普通玩家来的早,VIP玩家占用普通桌子
VIP[vip_Idx].start_time = ti;
VIP[vip_Idx].end_time = ti + VIP[vip_Idx].service_time;
Table[j] = VIP[vip_Idx].end_time;
//
t_reach = sec2all(VIP[vip_Idx].s_time);
t_start = sec2all(VIP[vip_Idx].start_time);
int cha = VIP[vip_Idx].start_time - VIP[vip_Idx].s_time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t_reach.hour, t_reach.minute, t_reach.sec, t_start.hour, t_start.minute, t_start.sec, int(cha * 1.0 / 60 + 0.5));
//
vip_Idx++;
num_of_player[j] ++;
}
else if (nor_Idx < nor_num && vip_Idx >= vip_num && Normal[nor_Idx].s_time <= ti){//只剩普通玩家
Normal[nor_Idx].start_time = ti;
Normal[nor_Idx].end_time = ti + Normal[nor_Idx].service_time;
Table[j] = Normal[nor_Idx].end_time;
//
t_reach = sec2all(Normal[nor_Idx].s_time);
t_start = sec2all(Normal[nor_Idx].start_time);
int cha = Normal[nor_Idx].start_time - Normal[nor_Idx].s_time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t_reach.hour, t_reach.minute, t_reach.sec, t_start.hour, t_start.minute, t_start.sec, int(cha * 1.0 / 60 + 0.5));
//
nor_Idx++;
num_of_player[j] ++;
}
else if (nor_Idx >= nor_num && vip_Idx < vip_num && VIP[vip_Idx].s_time <= ti){//只剩VIP玩家
VIP[vip_Idx].start_time = ti;
VIP[vip_Idx].end_time = ti + VIP[vip_Idx].service_time;
Table[j] = VIP[vip_Idx].end_time;
//
t_reach = sec2all(VIP[vip_Idx].s_time);
t_start = sec2all(VIP[vip_Idx].start_time);
int cha = VIP[vip_Idx].start_time - VIP[vip_Idx].s_time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t_reach.hour, t_reach.minute, t_reach.sec, t_start.hour, t_start.minute, t_start.sec, int(cha * 1.0 / 60 + 0.5));
//
vip_Idx++;
num_of_player[j] ++;
}
}
}
}
//------找到下一个判断时间,这样可以防止超时--------------
//------它是桌子满的情况下的最早桌子的结束时间,或桌子空的情况下最早玩家的到达时间-----
int min = 100000;
bool full = true;
for (int i = 0; i < m; i++){
if (Table[i] == 0){
full = false;
}
else if(Table[i] < min)
min = Table[i];
}//找到最早的桌子结束时间min
if (full == false){//如果桌子有空,则找到最早玩家的到达时间
if (nor_Idx <= nor_num && min > Normal[nor_Idx].s_time){
min = Normal[nor_Idx].s_time;
}
if (vip_Idx <= vip_num && min > VIP[vip_Idx].s_time){
min = VIP[vip_Idx].s_time;
}
}
if (min - 1 > ti)
ti = min - 1;//下一个循环时间
// ----------------------------------
}//time
for (int i = 0; i < m; i++){//输出每张桌子的服务人数
if (i == m - 1)printf("%d\n", num_of_player[i]);
else printf("%d ", num_of_player[i]);
}
}
return 0;
}
截图:
从错误到全部正确经历了一个痛苦的过程
——Apie陈小旭