1. 程式人生 > >PAT 1026 Table Tennis

PAT 1026 Table Tennis

rst pat esp out his cal != output urn

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

註意點: 留意題中的條件, 每個人用桌的時間不能超過120分鐘
 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 struct Node{
 6   int arrivalTime, beginTime, endTime, serveTime;
 7   Node(int arrival, int serve){
 8     arrivalTime = arrival;
 9     serveTime = serve;
10   }
11 };
12 
13 bool cmp(Node& a, Node& b){return a.arrivalTime<b.arrivalTime;}
14 bool cmp1(Node& a, Node& b){return a.beginTime<b.beginTime;}
15 
16 int main(){
17   int n, i, j;
18   //v1記錄普通人, v2記錄會員, v記錄合並後的結果
19   vector<Node> v1, v2, v;
20   scanf("%d", &n);
21   for(i=0; i<n; i++){
22     int h, m, s, t, vip;
23     scanf("%d:%d:%d %d %d", &h, &m, &s, &t, &vip);
24     //我的天啊 找了好久的bug ,居然在這裏, 每個人的用桌時間不能超過120min
25     if(t>120) t = 120; 
26     if(vip==0) v1.push_back(Node(h*3600+60*m+s, t*60));
27     else v2.push_back(Node(h*3600+60*m+s, t*60));
28   }
29   sort(v1.begin(), v1.end(), cmp);
30   sort(v2.begin(), v2.end(), cmp);  
31   int k, m;
32   scanf("%d%d", &k, &m);
33   /*
34     table: 記錄該桌結束的時間
35     vipTable: 記錄該桌是否是vip桌
36     tableNum: 記錄該桌服務的人數
37   */
38   vector<int> vipTable(k+1, false), tableNum(k+1, 0), table(k+1, 8*3600);
39   for(i=0; i<m; i++){
40     int temp;
41     scanf("%d", &temp);
42     vipTable[temp] = true;
43   }
44   int idx1=0, idx2=0;
45   for(i=0; i<n; i++){
46     int firstEnd=1, firstvip=-1;
47     for(j=1; j<=k; j++) //先找到結束時間最早的, 編號最小的桌號
48       if(table[firstEnd]>table[j]) firstEnd = j;
49     for(j=1; j<=k && firstvip==-1; j++)//找到結束時間最早的, 編號最小的, vip桌的桌號
50       if(table[firstEnd]==table[j] && vipTable[j]) firstvip = j;
51       /*
52         三種情況下 vip會員占據球桌
53         1.已經沒有普通人
54         2.vip到達時間比普通人到達時間早 選擇結束時間最早的  編號最小的桌子
55         3.vip到達時間比普通人到達時間晚, 但是有vip球桌空閑 選擇編號最小的vip桌子
56       */
57     if(idx1>=v1.size() || (firstvip!=-1 && idx2<v2.size() && (v2[idx2].arrivalTime<=table[firstvip] )) || (idx2<v2.size() && v2[idx2].arrivalTime<v1[idx1].arrivalTime) ){
58         if(firstvip!=-1) firstEnd = firstvip;
59         
60         v2[idx2].beginTime = table[firstEnd]<v2[idx2].arrivalTime ? v2[idx2].arrivalTime : table[firstEnd];
61       table[firstEnd] = v2[idx2].beginTime + v2[idx2].serveTime;
62       v2[idx2].endTime = table[firstEnd];
63         if(v2[idx2].beginTime<21*3600){ //在21點之前得到桌子試用權的才能被記錄
64             v.push_back(v2[idx2]);
65             tableNum[firstEnd]++;
66          }
67       idx2++;
68     }else{
69         v1[idx1].beginTime = table[firstEnd]<v1[idx1].arrivalTime ? v1[idx1].arrivalTime : table[firstEnd];
70       table[firstEnd]  = v1[idx1].beginTime + v1[idx1].serveTime;
71       v1[idx1].endTime = table[firstEnd];
72         if(v1[idx1].beginTime<21*3600){
73             v.push_back(v1[idx1]);
74             tableNum[firstEnd]++;
75            }
76       idx1++;
77     }
78   }
79   sort(v.begin(), v.end(), cmp1);
80   for(i=0; i<v.size(); i++)
81     printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", v[i].arrivalTime/3600, v[i].arrivalTime%3600/60, v[i].arrivalTime%60, v[i].beginTime/3600, v[i].beginTime%3600/60, v[i].beginTime%60, (v[i].beginTime-v[i].arrivalTime+30)/60);
82   printf("%d", tableNum[1]);
83   for(i=2; i<=k;  i++) printf(" %d", tableNum[i]);
84   return 0;
85 }

PAT 1026 Table Tennis