1026. Table Tennis (30) [sorting + simulation + complicated logic] -- PAT (Advanced Level) Practise
Question Information
1026. Table Tennis (30)
Time limit 400 MS
The memory limit is 65536 kB.
Code length limit: 16000 B
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
Solutions
Maintain user queues, idle table queues, and table expiration queues.
NOTE: If it exceeds two hours, it must be truncated. The waiting time must be rounded in. Wait before and close.
AC code
# Include
# Include
# Include
# Include
# Include
Using namespace std; bool table [10005], isvip [10005]; int tableTimes [10005]; struct node {int arriveTime, useTime; bool vip; node () {} node (int t, int tu, bool v): arriveTime (t), useTime (tu), vip (v) {} bool operator <(const node & B) const {return arriveTime <B. arriveTime ;}}; bool operator <(const pair
& A, const pair
& B) {return a. first <B. first;} set
ArriveList; // Customer Arrival list priority_queue
, Vector
>, Greater
> Que; // set of the table tennis table expiration queue
FreeList; // The int main () {int n, tableNum, tableVipNum, tableVipNow, a, B, c, t, vip; int nowTime = 8*3600; scanf ("% d", & n); for (int I = 0; I <n; ++ I) {scanf ("% d: % d ", & a, & B, & c, & t, & vip); arriveList. insert (node (a * 3600 + B * 60 + c, t, vip = 1);} scanf ("% d", & tableNum, & tableVipNum ); for (int I = 0; I <tableNum; ++ I) {freeList. insert (I); // initialize the idle instance} tableVipNow = tableVipNum; for (int I = 0; I <tableVipNum; ++ I) {scanf ("% d", & vip); isvip [vip-1] = true;} while (! ArriveList. empty () & nowTime <21*3600) {t = arriveList. begin ()-> arriveTime; if (! FreeList. empty () & t <= nowTime) {if (tableVipNow> 0) {set
: Iterator it = arriveList. begin (); while (it! = ArriveList. end () & nowTime> = it-> arriveTime) {if (it-> vip) {break ;}++ it ;}if (it! = ArriveList. end () & nowTime> = it-> arriveTime) {-- tableVipNow; set
: Iterator itable = freeList. begin (); while (! Isvip [* itable]) {++ itable;} t = it-> arriveTime; printf ("% 02d: % 02d: % 02d % 02d: % 02d: % 02d: % 02d % d \ n ", t/3600, t % 3600/60, t % 60, nowTime/3600, nowTime % 3600/60, nowTime % 60, (nowTime-t + 30) /60); // rounding tableTimes [* itable] ++; que. push (make_pair (nowTime + min (120, it-> useTime) * 60, * itable); // truncates freeList for more than two hours. erase (* itable); arriveList. erase (* it); continue;} printf ("% 02d: % 02d: % 02d % 02d: % 02d: % 02d % d \ n ", T/3600, t % 3600/60, t % 60, nowTime/3600, nowTime % 3600/60, nowTime % 60, (nowTime-t + 30)/60); tableTimes [* freeList. begin ()] ++; que. push (make_pair (nowTime + min (120, arriveList. begin ()-> useTime) * 60, * freeList. begin (); // truncates if (isvip [* freeList. begin ()]) {-- tableVipNow;} freeList. erase (* freeList. begin (); arriveList. erase (* arriveList. begin (); continue;} else {if (que. empty () | (! FreeList. empty () & que. top (). first> t) {nowTime = t;} else {if (nowTime <que. top (). first) {nowTime = que. top (). first;} while (! Que. empty () & nowTime> = que. top (). first) {freeList. insert (que. top (). second); if (isvip [que. top (). second]) {++ tableVipNow;} que. pop () ;}}} printf ("% d", tableTimes [0]); for (int I = 1; I <tableNum; ++ I) {printf ("% d", tableTimes [I]);} return 0 ;}