1026. Table Tennis (30)

来源:互联网 发布:java的对称杨辉三角 编辑:程序博客网 时间:2024/05/16 13:53

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:
920:52:00 10 008:00:00 20 008:02:00 30 020:51:00 10 008:10:00 5 008:12:00 10 120:50:00 10 008:01:30 15 120:53:00 10 13 12
Sample Output:
08:00:00 08:00:00 008:01:30 08:01:30 008:02:00 08:02:00 008:12:00 08:16:30 508:10:00 08:20:00 1020:50:00 20:50:00 020:51:00 20:51:00 020:52:00 20:52:00 03 3 2

    这道题给出若干对人来到乒乓球场的时间和他们打的时间,以及给出他们是否有vip卡,vip的功能就是如果有一个vip桌子进入空的状态,则队伍中的第一个vip可以插队。输出每对人到来的时间,开始打的时间和等待的时间,最后输出每张桌子服务过的人数。这道题条件很多,也很烦,注意的点也很多,比如打的时间超过2小时的只能打2小时,还有就是如果一个人能选多张桌子,则按照桌子顺序选择序号最小的(如果有vip桌子可以选择,vip玩家优先选择vip桌子)。

    这里可以分成人选桌子和桌子选人两种情况。先将人按时间顺序排好,用isserved记录好已经打过的人,用end_time记录各个桌子当前玩家结束的时间。选出还没打过的人之中来的最早的,如果当前玩家有桌子可以选择,就是人选桌子的情况,则按照上一段的规则选择桌子;如果没有的选则是要等待桌子(等待最快结束的桌子),等待过程中可能来了别的玩家,所以就是桌子选人的情况,如果在该桌子结束前来了有vip玩家,则选择vip玩家,如果没有则选择最早来的;然后用玩家开始的时间加上玩家要打的时间更新桌子的结束时间。以此类推,注意超过21点开始的玩家忽略之。

    花了一晚上做这道题真是心酸...


代码:

#include <iostream>#include <cstring>#include <vector>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;#define INF 1<<30#define END 13*60*60struct player{int arrive;int cost;int isvip;int start;};bool cmp1(const player &p1,const player &p2){return p1.arrive<p2.arrive;}bool cmp2(const player &p1,const player &p2){return p1.start<p2.start;}int get_time(string t){int h=atoi(t.substr(0,2).c_str());int m=atoi(t.substr(3,2).c_str());int s=atoi(t.substr(6,2).c_str());return (h-8)*60*60+m*60+s;}int main(){int n;cin>>n;vector<player>players(n);for(int i=0;i<n;i++){string arrive;cin>>arrive>>players[i].cost>>players[i].isvip;players[i].arrive=get_time(arrive);players[i].cost*=60;if(players[i].cost>2*60*60) players[i].cost=2*60*60;}sort(players.begin(),players.end(),cmp1);int tn,vtn;cin>>tn>>vtn;vector<int>serve_num(tn,0);vector<int>end_time(tn,0);vector<bool>isvt(tn,false);vector<bool>isserved(n,false);int min_vt=INF;for(int i=0;i<vtn;i++) {int t;cin>>t;min_vt=min(min_vt,t-1);isvt[t-1]=true;}for(int i=0;i<n;i++){int j,Min=INF,t_idx,p_idx;for(p_idx=0;p_idx<n;p_idx++){if(!isserved[p_idx]) break;}for(j=0;j<tn;j++){if(end_time[j]<Min){Min=end_time[j];t_idx=j;}if(players[p_idx].arrive>=end_time[j]){t_idx=j;break;}}if(players[p_idx].arrive>=end_time[t_idx]){if(players[p_idx].isvip==1) {for(int j=t_idx;j<tn;j++){if(isvt[j]&&players[p_idx].arrive>=end_time[j]){t_idx=j;break;}}}players[p_idx].start=players[p_idx].arrive;}else{int ind=-1;for(int j=p_idx;j<n;j++){if(isserved[j]) continue;if(players[j].arrive<=Min&&isvt[t_idx]&&players[j].isvip==1) {ind=j;break;}if(ind==-1){ind=j;}}p_idx=ind;players[p_idx].start=Min;}end_time[t_idx]=players[p_idx].start+players[p_idx].cost;if(players[p_idx].start<END) serve_num[t_idx]++;isserved[p_idx]=true;}sort(players.begin(),players.end(),cmp2);for(int i=0;i<n;i++){if(players[i].start>=END) break;int t1=players[i].arrive,t2=players[i].start,t=t2-t1;printf("%02d:%02d:%02d ",t1/3600+8,(t1%3600)/60,t1%60);printf("%02d:%02d:%02d ",t2/3600+8,(t2%3600)/60,t2%60);printf("%d\n",(t+30)/60);}cout<<serve_num[0];for(int i=1;i<tn;i++){cout<<" "<<serve_num[i];}}




0 0
原创粉丝点击