• PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)


    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
    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

    题意:

    题目大意: 一个乒乓球俱乐部有N个球员和K个乒乓球桌台,俱乐部8点到21点开门,N个球员不同时间到达俱乐部,根据他们的到达时间和打球时间按照乒乓球台号码从小到大分配球台。原本是一个极其简单的模拟队列问题,但是题目加了一句话,使得这道题的逻辑瞬间复杂了起来——这句话增加了一些约束条件:

      有VIP球桌和VIP会员:1. 如果VIP球桌空闲,且到场的有VIP队员,那么VIP在排队等候的时候优先于一般会员。2. 如果没有VIP会员,VIP球桌空闲的话,一般用户也可以使用,先到先得;3. 如果没有空闲的VIP球桌,VIP会员和一般会员一样排队。

    解题思路:

    1.使用优先队列,排队的时候,队列a,b中按到达时间排序。输出时,队列ans中按服务时间排序。

    2.核心逻辑:

    先找出当前桌子中最先结束的时间min_t,最先结束的桌子min_k

    if min_k是vip桌子的话:

      看看vip队列中有没有人,且符合条件(到达时间早于min_t)

        if 有且符合条件

          替换min_k

        continue

    else:

      把a队列(普通用户队列)和b队列(vip队列)中的第一个人拿出来,比较谁更早一点

      (这里有个小技巧,如果某个队列为空了,那么从这个队列里取出来的人的到达时间为99999999,那么就不会取到他了)

      if 普通用户早:

        用它替换min_k

      else :(vip早)

        遍历所有桌子,有没有空闲的vip桌

          if 有vip桌 j 空闲:

            用它替换 j

        continue

        else:

          用它替换min_k

    坑点:

    ①playtime超过2小时要压缩为2小时

    ②虽然本题所有人的到达时间没有超过21点,但是第三组数据有恰好21点到的,此人不能得到服务

    ③vip用户,如果此时有vip桌子空着,用编号最小的vip桌子

    ④等待时间四舍五入

    测试样例:

    //vip桌子分配例子,有vip桌子时,优先把vip分到编号小的vip桌子,而不是编号小的vip桌子
    4
    06:00:00 30 1
    08:00:00 30 1
    10:00:00 30 1
    12:00:00 30 1
    5 1
    3
    答案为:
    06:00:00 08:00:00 120
    08:00:00 08:00:00 0
    10:00:00 10:00:00 0
    12:00:00 12:00:00 0
    1 0 3 0 0
    
    
    //边缘测试例子
    1
    21:00:00 10 1
    3 3
    1 2 3
    答案为:
    0 0 0
    
    //超过两小时的例子
    2
    18:00:00 180 1
    20:00:00 60 1
    1 1
    1
    答案为:
    18:00:00 18:00:00 0
    20:00:00 20:00:00 0
    2
    
    //关于四舍五入为1分钟的例子,大约等于30秒才+1分钟,小于30则不+
    3
    07:59:31 60 1
    07:59:29 60 1
    08:00:30 100 1
    2 1
    1
    答案为:
    1
    07:59:29 08:00:00 1
    07:59:31 08:00:00 0
    08:00:30 09:00:00 60
    2 1
    View Code

    AC代码:

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 struct node{
      4     int a;//到达的时间 
      5     int s;//开始的时间 
      6     int t;//服务时间 
      7     
      8 };
      9 struct cmp
     10 {
     11     bool operator()(node &x,node &y)
     12     {
     13         return x.a > y.a;
     14     }
     15 };
     16 struct cmp2
     17 {
     18     bool operator()(node &x,node &y)
     19     {
     20         return x.s > y.s;
     21     }
     22 };
     23 priority_queue<node ,vector<node>,cmp>a,b;//a为普通用户,b为vip用户 
     24 priority_queue<node ,vector<node>,cmp2>ans;
     25 int n,m,k; 
     26 int tab[105];//桌子服务的人数
     27 int vt[105];//标记是不是vip桌子 
     28 node q[105];//正在服务的人们 
     29 int main(){
     30     cin>>n;
     31     memset(vt,0,sizeof(vt));
     32     memset(tab,0,sizeof(tab));
     33     while(!a.empty()) a.pop();
     34     while(!b.empty()) b.pop();
     35     while(!ans.empty()) ans.pop();
     36     for(int i=1;i<=n;i++){
     37         int hh,mm,ss;
     38         node x;
     39         scanf("%d:%d:%d",&hh,&mm,&ss);
     40         hh=hh*3600+mm*60+ss;
     41         cin>>mm;
     42         mm*=60;
     43         if(mm>2*3600) mm=2*3600;
     44         x.a=hh;
     45         x.t=mm;        
     46         int v;
     47         cin>>v;
     48         if(x.a>=21*3600){//时间超过来的直接不要 
     49             continue;
     50         }
     51         if(v==0){
     52             a.push(x);
     53         }else{//vip 
     54             b.push(x);
     55         }
     56     }
     57     cin>>k>>m;    
     58     for(int i=1;i<=k;i++){//初始化
     59         q[i].a=-1;//初始来的人时间为-1 
     60         q[i].s=8*3600;
     61         q[i].t=0; 
     62     }    
     63     for(int i=1;i<=m;i++){
     64         int x;
     65         cin>>x;
     66         vt[x]=1;
     67     }
     68     while(!a.empty() || !b.empty())//开始执行 
     69     {        
     70         int min_t=99999999;//找到一个最早的结束时间
     71         int min_k=-1;//最早结束时间对于的桌号 
     72         for(int i=1;i<=k;i++){
     73             if(min_t>q[i].s+q[i].t){
     74                 min_t=q[i].s+q[i].t;
     75                 min_k=i;
     76             }
     77         } 
     78         if(min_t>=21*3600){
     79             break;
     80         }
     81         //【1】先检查是不是vip桌子
     82         if(vt[min_k]){        
     83             if(!b.empty()){//先看看队列里有没有vip
     84                 node x=b.top();
     85                 if(x.a<min_t){//如果vip在队列里,优先考虑 
     86                     ans.push(q[min_k]);//把排在min_k的人放进最终答案中 
     87                     q[min_k].a=x.a;
     88                     q[min_k].t=x.t;
     89                     q[min_k].s=min_t;
     90                     tab[min_k]++;//对于桌子服务的人数++    
     91                     b.pop();
     92                     continue;
     93                 }                
     94             }
     95         } 
     96         //【2】不是vip桌子 
     97         node x,y;
     98         if(!a.empty()){
     99             x=a.top();
    100         }else{//小技巧,空的话就99999999
    101             x.a=99999999;
    102         }
    103         if(!b.empty()){
    104             y=b.top();
    105         }else{
    106             y.a=99999999;
    107         }        
    108         if(x.a<y.a){//两者谁先就放谁
    109             ans.push(q[min_k]);
    110             tab[min_k]++;
    111             q[min_k].a=x.a;
    112             q[min_k].t=x.t;
    113             a.pop();
    114             if(x.a<min_t){    
    115                 q[min_k].s=min_t;            
    116             }else{
    117                 q[min_k].s=x.a;
    118             }
    119         }else{//如果是vip早的话,看看有没有同样结束的vip桌子
    120             //(1)坑点!优先把vip分到编号小的vip桌子,而不是编号小的桌子     
    121             int flag=0;
    122             for(int j=1;j<=k;j++){
    123                 if(vt[j] && y.a >= q[j].s+q[j].t){//有符合条件的桌子 
    124                     ans.push(q[j]);
    125                     tab[j]++;
    126                     q[j].a=y.a;
    127                     q[j].t=y.t;
    128                     q[j].s=y.a;
    129                     b.pop();                                        
    130                     flag=1;
    131                     break;    
    132                 }
    133             } 
    134             if(flag) continue;
    135             //(2) 否则,就放在min_k这张桌子上 
    136             ans.push(q[min_k]);
    137             tab[min_k]++;
    138             q[min_k].a=y.a;
    139             q[min_k].t=y.t;
    140             b.pop();
    141             if(y.a<min_t){    
    142                 q[min_k].s=min_t;            
    143             }else{
    144                 q[min_k].s=y.a;
    145             }    
    146         }        
    147     }
    148     for(int i=1;i<=k;i++){
    149         ans.push(q[i]);
    150     }
    151     //输出
    152     while(!ans.empty()){
    153         node x = ans.top();
    154         ans.pop();
    155         
    156         if(x.a==-1){//一开始是默认没有人的 
    157             continue;
    158         }        
    159         int cha=x.s-x.a;//计算时间差 
    160         
    161         int hh=x.a/3600;
    162         x.a=x.a%3600;
    163         int mm=x.a/60;
    164         int ss=x.a%60;
    165         printf("%02d:%02d:%02d ",hh,mm,ss);
    166         
    167         hh=x.s/3600;
    168         x.s=x.s%3600;
    169         mm=x.s/60;
    170         ss=x.s%60;
    171         printf("%02d:%02d:%02d ",hh,mm,ss);
    172         
    173         cout<<int(cha*1.0/60+0.5)<<endl;//四舍五入,不是向上取整 
    174     }
    175     for(int i=1;i<=k;i++){
    176         cout<<tab[i];
    177         if(i!=k) cout<<" ";
    178     }
    179     return 0;
    180 } 
  • 相关阅读:
    stl 在 acm中的应用总结
    hdu_2089(数位dp)
    水dp第二天(背包有关)
    dp水一天
    poj_2195Going Home(最小费用最大流)
    poj_3281Dining(网络流+拆点)
    GSS4
    SPOJ GSS1_Can you answer these queries I(线段树区间合并)
    Ajax实现局部数据交互的一个简单实例
    对学习Ajax的知识总结
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270677.html
Copyright © 2020-2023  润新知