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
思路:
典型的事件驱动模拟
遇到的问题:
1. 有等待的vip选手,且有空闲的vip 球桌,则优先使用序号小的vip球桌,不然vip和普通选手一样;
2. 事件触发的优先级:触发事件->VIP->到达时间(最后一个测试点会涉及);
3. 持续时间需要截断(2hours);
#include <vector> #include <string> #include <algorithm> #include <queue> #include <deque> #include <cstdio> using namespace std; #define CLOSETIME 21*3600 #define OPENTIME 8*3600 struct table { table():vip_tag(0),count(0), busy(0){}; int vip_tag; int count; int busy; }; struct player { player():arrive_h(0),arrive_m(0),arrive_s(0), during_time(0), vip(0), table(-1){}; int arrive_h; int arrive_m; int arrive_s; int during_time; int vip; int table; }; struct Event { Event(player p, int h, int m, int s, int t):p(p),occur_h(h), occur_m(m), occur_s(s),type(t){}; player p; int occur_h; int occur_m; int occur_s; int type; }; struct cmp { bool operator()(Event &a, Event &b) { int t1 = a.occur_h*3600 + a.occur_m* 60 + a.occur_s; int t2 = b.occur_h*3600 + b.occur_m*60 + b.occur_s; if(t1 == t2) {//事件触发事件优先 if(a.p.vip == b.p.vip) {//对于最后一个case很重要, 事件触发时间相同的情况下VIP优先,VIP相同的情况下到达时间优先 int at1 = a.p.arrive_h* 3600+a.p.arrive_m*60+a.p.arrive_s; int at2 = b.p.arrive_h* 3600+b.p.arrive_m*60+b.p.arrive_s; return at1 > at2; } return a.p.vip < b.p.vip; } return t1 > t2; } }; int main() { int N; scanf("%d", &N); vector<player> players(N); int i; for(i = 0; i < N; i++) { player &t = players[i]; scanf("%d:%d:%d %d %d", &t.arrive_h, &t.arrive_m,&t.arrive_s, &t.during_time,&t.vip); if(t.during_time > 120) t.during_time = 120; } int K, M; scanf("%d %d", &K, &M); vector<table> tables(K); for(i = 0; i< M; i++) { int t; scanf("%d", &t); tables[t-1].vip_tag = 1; } priority_queue<Event,vector<Event>,cmp> eq; deque<Event> q; for(i = 0; i< N; i++) { int tt = 3600*players[i].arrive_h+60*players[i].arrive_m+players[i].arrive_s; if(tt < CLOSETIME && tt >= OPENTIME) eq.push(Event(players[i],players[i].arrive_h,players[i].arrive_m, players[i].arrive_s, 0)); } while(!eq.empty()) { Event e = eq.top(); eq.pop(); if(e.type == 0) {//arrive event int j; if(e.p.vip) {//VIP prio to select vip table firstly int v = -1; for(j = 0; j< K; j++) {//find a free table if(!tables[j].busy ) { if(v == -1) v = j; if(tables[j].vip_tag) break; } } if(j == K) if(v != -1) j = v; } else { for(j = 0; j< K; j++) {//find a free table if(!tables[j].busy) break; } } if(j != K) {//if j is a free table tables[j].busy = 1; tables[j].count++; e.p.table = j; int during_time = e.p.during_time; int s = e.occur_s; int m = e.occur_m+during_time; int h = e.occur_h + m / 60; eq.push(Event(e.p, h,m %60, s, 1)); //output the result int wait_t = ( 3600*e.occur_h+60*e.occur_m+e.occur_s - 3600*e.p.arrive_h-60*e.p.arrive_m-e.p.arrive_s+30)/60; printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",e.p.arrive_h,e.p.arrive_m,e.p.arrive_s,e.occur_h,e.occur_m,e.occur_s, wait_t); } else {// if there is no free table q.push_back(e); } } else {//leave event int h = e.occur_h, m = e.occur_m, s = e.occur_s; player &p = e.p; tables[p.table].busy = 0; if(!q.empty()) { int v = 0; if(tables[p.table].vip_tag) { for(v = 0; v < q.size(); v++) { if(q[v].p.vip) break; } if(v == q.size()) v = 0; } int tt = 3600*h + 60*m + s; if(tt < CLOSETIME) { eq.push(Event(q[v].p, h,m,s,0)); q.erase(q.begin()+v); } else { break; } } } } int flag = 0; for(i=0; i< tables.size(); i++) { if(!flag) { flag = 1; printf("%d",tables[i].count); } else printf(" %d",tables[i].count); } printf("\n"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。