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
发现这道题是一年前刚学C的时候的一次大作业,于是翻了半天翻出了当年写的C的代码提交上去,结果CASE全过了。暂时就懒得再去重新做一遍这道题了。把当年写的难看的两百多行代码发出来吧,以后有空再来重新做一遍这道题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define pMAX 10000
#define tMAX 100
typedef struct players PtrPlayer;
typedef struct tables PtrTable;
struct players{
char arrive_time[9];
char server_time[9];
int play_time;
int VIP_tag; //0 represents isn't VIP, 1 represents is VIP
int del; //0 represents haven't serverd, 1 represents have already serverd
};
struct tables{
int IsVIP; //0 represents isn't VIP, 1 represents is VIP
int server_num;
char free_time[9];
};
PtrTable t[tMAX+1];
PtrPlayer p[pMAX+1];
int tvip_num[tMAX]; //to save the VIP tables' number
int pvip_num[pMAX+1]; //to save the VIP players' number
void add(PtrTable *a,char *p, int num) //add servered time and play time
{
int h[2],s[2],second,hour,ts,th;
if(num > 120) // can't play more than two hours
num = 120;
if(strcmp(a->free_time,p)<0)
strcpy(a->free_time,p);
hour = (a->free_time[0]-'0')*10+(a->free_time[1]-'0');
second = (a->free_time[3]-'0')*10+(a->free_time[4]-'0');
ts = second + num;
th = ts/60+hour;
ts %= 60;
h[0] = th/10;
h[1] = th%10;
s[0] = ts/10;
s[1] = ts%10;
a->free_time[0] = '0'+h[0];
a->free_time[1] = '0'+h[1];
a->free_time[3] = '0'+s[0];
a->free_time[4] = '0'+s[1];
if(a->free_time[0]=='2'&& a->free_time[1]>'0'){ // can't play more than 21:00:00
a->free_time[1]='1';
a->free_time[3]='0';
a->free_time[4]='0';
a->free_time[6]='0';
a->free_time[7]='0';
}
}
int cmp(const void *a,const void *b) //sort by the arrive time
{
return strcmp(((PtrPlayer *)a)->arrive_time,((PtrPlayer *)b)->arrive_time);
}
int find_table(PtrTable t[],int K,char time_now[])
{
int i;
for(i = 1; i <= K; i++)
{
if(strcmp(t[i].free_time,time_now)<=0)
return i;
}
return 0;
}
int find_VIPtable(PtrTable t[],int tvip_num[],int M,char time_now[])
{
int i;
for(i = 1; i <= M; i++)
{
if(strcmp(t[tvip_num[i]].free_time,time_now)<=0)
return tvip_num[i];
else
return 0;
}
}
int find_earliest(PtrTable t[],int K)
{
int i,min=1;
for(i = 1; i <= K; i++)
{
if(strcmp(t[i].free_time,t[min].free_time)<0)
min = i;
}
return min;
}
int find_VIPplayer(PtrPlayer p[],PtrTable t[],int pvip_num[],int *ptr_pvip,int norm_num)
{
while(pvip_num[*ptr_pvip] && (strcmp(p[pvip_num[*ptr_pvip]].arrive_time,t[norm_num].free_time)<=0))
{
if(p[pvip_num[*ptr_pvip]].del==1)
(*ptr_pvip)++;
else
return pvip_num[(*ptr_pvip)++];
}
return 0;
}
void distribute(PtrPlayer *p,PtrTable *t)
{
int min;
if(strcmp(t->free_time,"21:00:00")>=0)
return 0;
strcpy(p->server_time,t->free_time);
p->del = 1;
t->server_num++;
min = wait_time(p->arrive_time,t->free_time);
if(strcmp(p->arrive_time,t->free_time)>0)
printf("%s %s %d\n",p->arrive_time,p->arrive_time,min);
else
printf("%s %s %d\n",p->arrive_time,t->free_time,min);
add(t,p->arrive_time,p->play_time);
}
int wait_time(char arr[],char fre[])
{
int i,second=0,minute=0;
if(strcmp(fre,arr)<=0)
return 0;
else
{
second += ((fre[0]-arr[0])*10+fre[1]-arr[1])*3600;
second += ((fre[3]-arr[3])*10+fre[4]-arr[4])*60;
second += ((fre[6]-arr[6])*10+fre[7]-arr[7]);
if(second%60<30)
minute = second/60;
else
minute = second/60+1;
return minute;
}
}
int main(void)
{
int N,K,M;
int i,ptr_pvip=1;
int norm_num,VIP_num,vip_player;
int flag;
//FILE *fp;
//fp=fopen("C:\\Users\\Bloody_mercy\\Desktop\\bonus\\8.in","r");
//fscanf(fp,"%d",&N);
scanf("%d",&N);
for(i = 1; i <= N; i++){
//fscanf(fp,"%s%d%d",&p[i].arrive_time,&p[i].play_time,&p[i].VIP_tag);
scanf("%s%d%d",&p[i].arrive_time,&p[i].play_time,&p[i].VIP_tag);
strcpy(p[i].server_time,"00:00:00"); //initialize the server time
p[i].del = 0; //that means this pair haven't serverd
}
qsort(&p[1],N,sizeof(struct players),cmp); //sort the players
for(i = 1; i <=N; i++)
{
if(p[i].VIP_tag)
pvip_num[ptr_pvip++]=i;
}
pvip_num[ptr_pvip]=0;
ptr_pvip = 1;
//fscanf(fp,"%d%d",&K,&M);
scanf("%d%d",&K,&M);
for(i = 1; i <= K; i++) //initialize the table
{
strcpy(t[i].free_time,"08:00:00");
t[i].IsVIP = 0; //that means this table is not VIP
t[i].server_num = 0;
}
for(i = 1; i <= M; i++)
{
//fscanf(fp,"%d",&tvip_num[i]);
scanf("%d",&tvip_num[i]);
t[tvip_num[i]].IsVIP = 1;
}
tvip_num[i]=0;
//above is intialization
//starts
for(i = 1; i <= N;i++)
{
if(strcmp(p[i].arrive_time,"21:00:00")>=0) //the club is closed
break;
if(p[i].del==1) //this pair have been already serverd
continue;
flag = 0;
if(p[i].VIP_tag==1) // they are VIP players
{
VIP_num = find_VIPtable(t,tvip_num,M,p[i].arrive_time);
if(VIP_num){ //if find a VIP table available
distribute(&p[i],&t[VIP_num]);
continue;
}
else
{
norm_num = find_table(t,K,p[i].arrive_time);
if(norm_num){ // find a normal table
distribute(&p[i],&t[norm_num]);
continue;
}
else // wait and find the ealiest table
{
norm_num = find_earliest(t,K);
distribute(&p[i],&t[norm_num]);
continue;
}
}
}
else // they are not VIP players
{
norm_num = find_table(t,K,p[i].arrive_time);
if(norm_num){ // find a table
distribute(&p[i],&t[norm_num]);
continue;
}
else
{
norm_num = find_earliest(t,K);
if(t[norm_num].IsVIP == 1) // the earliest table is a VIP table
{
while(t[norm_num].IsVIP == 1)
{
vip_player = find_VIPplayer(p,t,pvip_num,&ptr_pvip,norm_num);
if(vip_player) // find a VIP player in queue
{
distribute(&p[vip_player],&t[norm_num]);
norm_num = find_earliest(t,K);
continue;
}
else // there is no VIP player in queue
{
distribute(&p[i],&t[norm_num]);
flag = 1;
break;
}
}
if(flag == 0)
distribute(&p[i],&t[norm_num]);
}
else
distribute(&p[i],&t[norm_num]);
}
}
}
for(i = 1; i <K; i++)
printf("%d ",t[i].server_num);
printf("%d",t[i].server_num);
return 0;
}