Pat1026代码

题目描写叙述:

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:

  1. 9
  2. 20:52:00 10 0
  3. 08:00:00 20 0
  4. 08:02:00 30 0
  5. 20:51:00 10 0
  6. 08:10:00 5 0
  7. 08:12:00 10 1
  8. 20:50:00 10 0
  9. 08:01:30 15 1
  10. 20:53:00 10 1
  11. 3 1
  12. 2

Sample Output:

  1. 08:00:00 08:00:00 0
  2. 08:01:30 08:01:30 0
  3. 08:02:00 08:02:00 0
  4. 08:12:00 08:16:30 5
  5. 08:10:00 08:20:00 10
  6. 20:50:00 20:50:00 0
  7. 20:51:00 20:51:00 0
  8. 20:52:00 20:52:00 0
  9. 3 3 2


这个排序模拟题。真是延续了PAT坑人的一贯作风啊;

參考了网上的一些代码,下面几点须要注意:

1.当有多个乒乓球台空暇时,vip顾客到了会使用最小id的vip球台,而不是最小id的球台,測试下面用例:

  1. 2
  2. 10:00:00 30 1
  3. 12:00:00 30 1
  4. 5 1
  5. 3
  1. 输出正确结果应为:
  1. 10:00:00 10:00:00 0
  1. 12:00:00 12:00:00 0
  1. 0 0 2 0 0
  1.  
  1. 2.题目要求每对顾客玩的时间不超过2小时。那么当顾客要求玩的时间>2小时的时候,应该截断控制。測试下面用例:
  1. 2
  2. 18:00:00 180 1
  3. 20:00:00 60 1
  4. 1 1
  5. 1
  1. 输出的正确结果应为:
  1. 18:00:00 18:00:00 0
  1. 20:00:00 20:00:00 0
  1. 2
  1. 3.尽管题目中保证客户到达时间在08:00:0021:00:00之间。可是依据最后的8case来看,里面还是有不在这个时间区间内到达的顾客,所以建议还是稍加控制。測试下面用例:
  1. 1
  2. 21:00:00 80 1
  3. 1 1
  4. 1
  1. 输出的正确结果应为:
  1. 0
  1. 4.题目中说的round up to an integer minutes是严格的四舍五入。须要例如以下做:
  1. wtime = (stime - atime + 30) / 60
  1. 而不是:
  1. wtime = (stime - atime + 59) / 60
     AC代码:
  1. #include<cstdio>
  2. #include<vector>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class Player
  8. {
  9. public:
  10. int arrive;//arrive time
  11. int vip;//vip flag
  12. int playtime;//the time player play
  13. bool operator<(const Player r)const
  14. {
  15. return arrive<r.arrive;
  16. }
  17. };
  18.  
  19. class Table
  20. {
  21. public:
  22. int freetime;//the time table can be used
  23. int vip;//vip falg
  24. int ID;//the number of table
  25. int num;//the number of players the table serve
  26. bool operator<(const Table r)const
  27. {
  28. if(freetime!=r.freetime)
  29. return freetime<r.freetime;
  30. else
  31. return ID<r.ID;
  32. }
  33. };
  34.  
  35. bool cmp(Table l,Table r)
  36. {
  37. return l.ID<r.ID;
  38. }
  39.  
  40. int main(int argc,char *argv[])
  41. {
  42. Table t[105];
  43. vector<Player> p;
  44. vector<Player> waiting;
  45. int n,i,j;
  46. int k,m;
  47. scanf("%d",&n);
  48. for(i=0;i<n;i++)
  49. {
  50. int h,m,s;
  51. Player temp;
  52. scanf("%d:%d:%d %d %d",&h,&m,&s,&temp.playtime,&temp.vip);
  53. temp.arrive=h*3600+m*60+s;
  54. if(temp.playtime>120)
  55. temp.playtime=120;
  56. temp.playtime*=60;
  57. if(temp.arrive>=21*60*60||temp.arrive<8*60*60)
  58. continue;
  59. p.push_back(temp);
  60. }
  61. scanf("%d%d",&k,&m);
  62. for(i=1;i<=k;i++)
  63. {
  64. t[i].ID=i;
  65. t[i].vip=0;
  66. t[i].num=0;
  67. t[i].freetime=0;
  68. }
  69. for(i=0;i<m;i++)
  70. {
  71. int index;
  72. scanf("%d",&index);
  73. t[index].vip=1;
  74. }
  75. int timer=0;
  76. int cur=0;
  77. sort(p.begin(),p.end());
  78. sort(t+1,t+1+k);
  79. while(timer<21*60*60)
  80. {
  81. for(;cur<p.size();cur++)
  82. {
  83. if(p[cur].arrive<=timer)//假设此时没有空暇球桌,而且在timer
  84. waiting.push_back(p[cur]);//之前到达,player需在队列里等待
  85. else
  86. break;
  87. }
  88. if(!waiting.size())//假设等待对列为空。即此时有剩余球桌
  89. {
  90. if(cur<p.size())
  91. {
  92. timer=p[cur].arrive;
  93. for(i=1;i<=k;i++)//把无人使用球桌的freetime更新为timer
  94. {
  95. if(t[i].freetime<=timer)
  96. t[i].freetime=timer;
  97. }
  98. waiting.push_back(p[cur++]);
  99. }
  100. else
  101. break;
  102. }
  103. vector<Player>::iterator it;//find the first vip player in the queue
  104. for(it=waiting.begin();it!=waiting.end();it++)
  105. if(it->vip)
  106. break;
  107. int vipplayer=0;
  108. if(it!=waiting.end())
  109. vipplayer=1;
  110. int viptable=-1;//find the first vacant vip table
  111. for(i=1;i<=k&&t[i].freetime==timer;i++)
  112. {
  113. if(t[i].vip)
  114. {
  115. viptable=i;
  116. break;
  117. }
  118. }
  119. if(viptable>=1&&vipplayer)//队列中有vip客户,且有空暇vip桌子
  120. {
  121. int arrive=it->arrive;
  122. printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",arrive/3600,
  123. (arrive%3600)/60,arrive%60,timer/3600,(timer%3600)/60,timer%60,
  124. (timer-it->arrive+30)/60);
  125. t[viptable].freetime=timer+it->playtime;
  126. t[viptable].num++;
  127. waiting.erase(it);
  128. }
  129. else
  130. {
  131. int arrive=waiting[0].arrive;
  132. printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",arrive/3600,
  133. (arrive%3600)/60,arrive%60,timer/3600,(timer%3600)/60,timer%60,
  134. (timer-arrive+30)/60);
  135. t[1].freetime=timer+waiting[0].playtime;
  136. t[1].num++;
  137. waiting.erase(waiting.begin());
  138. }
  139. sort(t+1,t+1+k);//对桌子的空暇时间进行排序,否则对ID进行排序
  140. timer=t[1].freetime;//则每次都是第一个桌子先被使用
  141. }
  142. sort(t+1,t+1+k,cmp);//恢复桌子的序号
  143. printf("%d",t[1].num);
  144. for(i=2;i<=k;i++)
  145. printf(" %d",t[i].num);
  146. printf("\n");
  147.  
  148. return 0;
  149. }

Pat(Advanced Level)Practice--1026(Table Tennis)的更多相关文章

  1. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  2. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  5. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  6. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  7. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  8. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  9. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

  10. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

随机推荐

  1. WebDriver API——第1部分

    The API definitions in this chapter shows the absolute location of classes. However the recommended ...

  2. c++中的对象复制

    (1)this指针 this是一个隐含于每个类的成员函数的特殊指针,该指针是一个指向正在被某个成员函数操作的对象的指针. 当一个对象调用成员函数时,编译程序先将对象的地址赋给this指针,也就是说,当 ...

  3. HBuilder 打包 vue-cli 构建的 APP

    1.在打包之前需要修改一个地方,那就是config->index.js文件,修改assetsPublicPath: '/'为assetsPublicPath: './',截图如下 上面文件改好后 ...

  4. ios8设置application badge value

    在ios8中,直接设置application badge value会出错 [[UIApplication sharedApplication] setApplicationIconBadgeNumb ...

  5. WPF SL 属性生成器

    在开发WPF 和SL应用的时候通用会用到MVVM模式,每次写到类属性的时候要不断的写属性更新时通知方法,写多了就嫌烦,就手写了个属性生成工具,在属性更新的时候添加了更新通知方法. 工具中支持自定义类对 ...

  6. Android给TextView设置透明背景、圆角边框

    第一种方法:在drawable文件夹下新建一个文件设置背景样式 代码: 在drawable文件夹下面新建text_view_border.xml <?xml version="1.0& ...

  7. 接收广播BroadcastReceiver

    Broadcast Receiver用于接收并处理广播通知(broadcast announcements).多数的广播是系统发起的,如地域变换.电量不足.来电来信等.程序也可以播放一个广播.程序可以 ...

  8. ssh理论知识

    一.spring工作原理: 1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2.DispatcherSer ...

  9. unity, Awake的调用时机

    Awake是在setActive(true)时才会被调用,不过如果再setActive(false)然后重新setActive(true)的话,Awake就不会再被调用了,也就是说Awake能保证仅被 ...

  10. 三款免费的PHP加速器:APC、eAccelerator、XCache比较

    前几天给 Nova 的缓存系统添加了 Memcache 支持.但是这玩意儿对个人博客系统来说可能不太合适(哪天 XKLog 推出多用户版本了可能会派上用场),事实上,就我自己试用而言,单机情况下使用 ...