原题地址:http://www.patest.cn/contests/pat-a-practise/1026
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
解题思路:模拟题。过程如下:将所有的用户存到数组p按照到达时间排序,将vip用户单独存到一个数组vipline并按照到达时间排序。对p数组从前往后依次判断,若是vip用户,首先判断是否已经处理过(因为会有vip提前处理的情况),没有则在viptable查找空桌,有则占有,否则按照普通用户处理。对于普通用户,先找空桌或最先结束的桌,再判断该桌是否为viptable,是则判断vipline的下一用户是否符合vip先占用条件,不符合则被当前用户占用,符合则被vip占用,此用户重新进行业务判断。
模拟细节确实比较麻烦。
代码如下:
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <math.h>
#define MAXN 10005
typedef struct pairs{
char intime[10];
int inntime;
int stime;
int tag;
}pair;
pair p[MAXN];
pair vipline[MAXN];
int vipnum;
int tableT[100];
int tableC[100];
int isVip[100];
int n;
int k;
int parseTime(char* t){
return ((t[0]-'0')*10 + (t[1]-'8'))*60*60 + ((t[3]-'0')*10 + (t[4]-'0'))*60 + ((t[6]-'0')*10 + (t[7]-'0'));
}
int cmp(const void* _a, const void* _b){
pair a = *(pair*)_a;
pair b = *(pair*)_b;
return strcmp(a.intime,b.intime);
}
int main(){
freopen("1026.txt","r",stdin);
scanf("%d",&n);
vipnum = 0;
for(int i = 0; i < n; i++){
scanf("%s%d%d",&p[i].intime, &p[i].stime, &p[i].tag);
p[i].inntime = parseTime(p[i].intime);
if(p[i].stime > 120) p[i].stime = 120;
if(p[i].tag) {
strcpy(vipline[vipnum].intime, p[i].intime);
vipline[vipnum].inntime = p[i].inntime;
vipline[vipnum].stime = p[i].stime;
vipline[vipnum].tag = p[i].tag;
vipnum++;
}
}
qsort(p,n,sizeof(p[0]),cmp);
qsort(vipline,vipnum,sizeof(vipline[0]),cmp);
// for(int i = 0; i < n; i++) printf("%s-%d:%d:%d\n",p[i].intime,p[i].inntime,p[i].stime,p[i].tag);
// printf("=======================================\n");
// for(int i = 0; i < vipnum; i++) printf("%s:%d:%d\n",vipline[i].intime,vipline[i].stime,vipline[i].tag);
memset(tableT,0,sizeof(tableT));
memset(tableC,0,sizeof(tableC));
memset(isVip,0,sizeof(isVip));
int m;
scanf("%d%d",&k,&m);
for(int i = 0; i < m; i++){int t; scanf("%d",&t);isVip[t-1] = 1;}
// for(int i = 0; i < k; i++) printf("%d : " , isVip[i]);
int pv = 0;
for(int i = 0; i < n; i++){
int min = 900000;
int id = -1;
// printf("p[%d]-tag:%d\n",i,p[i].tag);
if(p[i].tag){
int flag = 0;
if(p[i].inntime < vipline[pv].inntime) continue; //{printf("@C1===\n"); continue;}
for(int j = 0; j < k; j++){
if(isVip[j] && tableT[j] <= p[i].inntime && p[i].inntime < 46800){
tableT[j] = p[i].inntime + p[i].stime*60;
tableC[j]++;
pv++;
flag = 1;
printf("%s %s 0\n",p[i].intime,p[i].intime);
break;
}
}
if(flag) continue;
// printf("FLAG:%d\n",flag);
}
int flag = 0;
for(int j = 0; j < k; j++){
if(tableT[j] <= p[i].inntime && p[i].inntime < 46800){
printf("%s %s %d\n",p[i].intime,p[i].intime, 0);
tableT[j] = p[i].inntime + p[i].stime*60;
tableC[j]++;
flag = 1;
break;
}
if(tableT[j] < min){
min = tableT[j];
id = j;
}
}
if(flag) continue;
if(p[i].inntime <= min && min < 46800){
int hour;
int minute;
int second;
second = tableT[id]%60;
minute = (tableT[id]%3600)/60;
hour = tableT[id]/3600 + 8;
if(!p[i].tag && isVip[id] && pv < vipnum && vipline[pv].inntime <= min){
printf("%s %02d:%02d:%02d %d\n",vipline[pv].intime,hour,minute,second,(min-vipline[pv].inntime+30)/60);
tableC[id]++;
tableT[id] += vipline[pv].stime*60;
pv++;
i--;
// printf("@C2===\n");
continue;
}else{
printf("%s %02d:%02d:%02d %d\n",p[i].intime,hour,minute,second,(min-p[i].inntime+30)/60);
tableC[id]++;
tableT[id] += p[i].stime*60;
}
}
}
int flag = 1;
for(int i = 0; i < k; i++){
if(flag) flag = 0;
else printf(" ");
printf("%d",tableC[i]);
}
return 0;
}