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张空闲的桌子,players来到后总是选择编号较小的桌子来进行练习,如果没有空闲的桌子可以使用,则需要排队等候,players能够使用桌子的最大时长为两小时。比较复杂的是,在这N张桌子中设置了m张专属于vip的桌子,如果等候队列中存在vip players,并且有空闲的vip table,则选择最前面的vip players使用vip table。如果队列中有vip players但是没有空闲的vip table则将vip players当做ordianry players来看待。

  给出每一组players的到达时间和使用时间,问在club的营业时间中每一组players的到达时间,开始时间,以及等待时间。并输出每个桌子的服务人数。

思路:

  模拟。构造两个结构体用来保存player和table的信息。将题目中的时间格式都化为second的形式来进行存储。解题的过程中用到了两个辅助函数update()和findNextVip(),第一个是用来更新player的开始时间以及当前player使用桌子的结束时间,第二个是用来寻找下一个到达的vip player的序号。将所有的用户根据到达时间进行排序。找到最早空闲出的table然后分别讨论这张桌子是不是vip table,然后再分别讨论当前的player是不是vip player根据不同的结果分别做不同的处理操作。

Code:

  1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 const int inf = 0x7fffffff;
6
7 int n, m, k, t;
8
9 struct Player {
10 int arriving_time;
11 int starting_time = inf;
12 int playing_time;
13 int isVip;
14 } players[10005];
15
16 struct Table {
17 int count = 0;
18 int end = 8 * 3600;
19 int isVip = 0;
20 } tables[105];
21
22 int parse(string str) {
23 int hh = stoi(str.substr(0, 2));
24 int mm = stoi(str.substr(3, 2));
25 int ss = stoi(str.substr(6, 2));
26 return hh * 3600 + mm * 60 + ss;
27 }
28
29 bool cmp1(Player a, Player b) { return a.arriving_time < b.arriving_time; }
30 bool cmp2(Player a, Player b) { return a.starting_time < b.starting_time; }
31
32 // 更新player的开始时间和table的end time
33 void update(int playerId, int tableId) {
34 if (players[playerId].arriving_time <= tables[tableId].end) {
35 players[playerId].starting_time = tables[tableId].end;
36 } else {
37 players[playerId].starting_time = players[playerId].arriving_time;
38 }
39 tables[tableId].end =
40 players[playerId].starting_time + players[playerId].playing_time;
41 tables[tableId].count++;
42 }
43
44 int findNextVip(int vipId) {
45 vipId++;
46 while (vipId < n && players[vipId].isVip == 0) vipId++;
47 return vipId;
48 }
49
50 int main() {
51 cin >> n;
52 string arriving_time;
53 int playing_time;
54 int isVip;
55 for (int i = 0; i < n; ++i) {
56 cin >> arriving_time >> playing_time >> isVip;
57 players[i].arriving_time = parse(arriving_time);
58 players[i].playing_time =
59 (playing_time > 120 ? 120 * 60 : playing_time * 60);
60 players[i].isVip = isVip;
61 }
62
63 sort(players, players + n, cmp1);
64
65 cin >> m >> k;
66 for (int i = 0; i < k; ++i) {
67 cin >> t;
68 tables[t].isVip = 1;
69 }
70
71 int i = 0, vipId = -1;
72 vipId = findNextVip(vipId);
73 while (i < n) {
74 int index = -1, minEndTime = inf;
75 for (int j = 1; j <= m; ++j) {
76 if (tables[j].end < minEndTime) {
77 index = j;
78 minEndTime = tables[j].end;
79 }
80 }
81 if (tables[index].end >= 21 * 3600) break;
82 if (players[i].isVip == 1 && i < vipId) {
83 i++;
84 continue;
85 }
86 // 如果是vip table
87 if (tables[index].isVip == 1) {
88 if (players[i].isVip == 1) {
89 update(i, index);
90 if (i = vipId) vipId = findNextVip(vipId);
91 ++i;
92 } else {
93 // 考虑所排队列中是否有vip player
94 if (vipId < n &&
95 players[vipId].arriving_time <= tables[index].end) {
96 update(vipId, index);
97 vipId = findNextVip(vipId);
98 } else {
99 update(i, index);
100 ++i;
101 }
102 }
103 } else {
104 if (players[i].isVip == 0) {
105 update(i, index);
106 ++i;
107 } else {
108 int vipIndex = -1, minVipEndTime = inf;
109 for (int j = 1; j <= m; ++j) {
110 if (tables[j].isVip == 1 && minVipEndTime > tables[j].end) {
111 vipIndex = j;
112 minVipEndTime = tables[j].end;
113 }
114 }
115 if (vipIndex != -1 &&
116 players[i].arriving_time >= tables[vipIndex].end) {
117 update(i, vipIndex);
118 if (vipId == i) vipId = findNextVip(vipId);
119 ++i;
120 } else {
121 update(i, index);
122 if (vipId == i) vipId = findNextVip(vipId);
123 ++i;
124 }
125 }
126 }
127 }
128 sort(players, players + n, cmp2);
129 for (int i = 0; i < n && players[i].starting_time < 21 * 3600; ++i) {
130 printf("%02d:%02d:%02d ", players[i].arriving_time / 3600,
131 players[i].arriving_time % 3600 / 60,
132 players[i].arriving_time % 60);
133 printf("%02d:%02d:%02d ", players[i].starting_time / 3600,
134 players[i].starting_time % 3600 / 60,
135 players[i].starting_time % 60);
136 printf("%.01d\n",
137 (players[i].starting_time - players[i].arriving_time + 59) / 60);
138 }
139 for (int i = 1; i <= m; ++i) {
140 if (i != 1) cout << " ";
141 cout << tables[i].count;
142 }
143 return 0;
144 }

有三组数据没有通过。

参考:

  https://www.liuchuo.net/archives/2955

1026 Table Tennis的更多相关文章

  1. PAT 1026 Table Tennis[比较难]

    1026 Table Tennis (30)(30 分) A table tennis club has N tables available to the public. The tables ar ...

  2. PAT甲级1026. Table Tennis

    PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...

  3. PAT 甲级 1026 Table Tennis(模拟)

    1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...

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

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

  5. PAT 1026. Table Tennis

    A table tennis club has N tables available to the public.  The tables are numbered from 1 to N.  For ...

  6. 1026. Table Tennis (30)

    题目如下: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. ...

  7. PAT 1026 Table Tennis (30)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  8. 1026 Table Tennis (30)(30 分)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  9. 1026 Table Tennis (30分)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  10. 1026 Table Tennis (30分) 难度不高 + 逻辑复杂 +细节繁琐

    题目 A table tennis club has N tables available to the public. The tables are numbered from 1 to N. Fo ...

随机推荐

  1. golang知识总结

    目录 1.slice扩容规则 2.内存寻址.内存对齐,go结构体内存对齐策略 3.go语言map类型分析 3.1 hash冲突 3.2 hash表扩容 3.3 go语言中的map结构是hash表. 3 ...

  2. 看动画轻松学会 Raft 算法

    由于 Paxos 算法过于晦涩难懂且难以实现,Diego Ongaro 提出了一种更易于理解和实现并能等价于 Paxos 算法的共识算法 - Raft 算法. 因为 Raft 算法清晰易懂越来越多的开 ...

  3. Nginx解析漏洞复现以及哥斯拉连接Webshell实践

    Nginx解析漏洞复现以及哥斯拉连接Webshell实践 目录 1. 环境 2. 过程 2.1 vulhub镜像拉取 2.2 漏洞利用 2.3 webshell上传 2.4 哥斯拉Webshell连接 ...

  4. 剑指 Offer 41. 数据流中的中位数 + 堆 + 优先队列

    剑指 Offer 41. 数据流中的中位数 Offer_41 题目详情 题解分析 本题使用大根堆和小根堆来解决这个寻找中位数和插入中位数的问题. 其实本题最直接的方法是先对数组进行排序,然后取中位数. ...

  5. 【海思】Hi3516A 运行sample_venc的demo内核奔溃(DDR问题)

    作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14514297.html 目录 一.前言 二.使用memtester对ddr进行压力测试 三.修改uboot的DD ...

  6. 用实战玩转pandas数据分析(一)——用户消费行为分析(python)

      CD商品订单数据的分析总结.根据订单数据(用户的消费记录),从时间维度和用户维度,分析该网站用户的消费行为.通过此案例,总结订单数据的一些共性,能通过用户的消费记录挖掘出对业务有用的信息.对其他产 ...

  7. [BJOI2020] 封印

    一.题目 点此看题 二.解法 今天不知道为什么手感这么好,写一发完全没调就过掉了. 我感觉这种多组询问的字符串题是很难的,经常没有什么思路.我先考虑了一下能不能像 区间本质不同的子串个数 这样直接离线 ...

  8. 攻防世界 reverse 进阶5-7

    5.re-for-50-plz-50  tu-ctf-2016 流程很简单,异或比较 1 x=list('cbtcqLUBChERV[[Nh@_X^D]X_YPV[CJ') 2 y=0x37 3 z= ...

  9. [React Hooks长文总结系列一]初出茅庐,状态与副作用

    写在开头 React Hooks在我的上一个项目中得到了充分的使用,对于这个项目来说,我们跳过传统的类组件直接过渡到函数组件,确实是一个不小的挑战.在项目开发过程中也发现项目中的其他小伙伴(包括我自己 ...

  10. (十四--十五)数据库查询优化Part I

    (十四--十五)数据库查询优化Part I 如果理解的有问题.欢迎大家指出.这也是我在看课记得笔记..可能会有很多问题 查询优化的重要性 请记住用户只会告诉DMBS他们想要什么样的结果,而不会告诉他们 ...