PAT甲级真题 1026 Table Tennis (30分) C++实现(窗口排队问题,细数多处坑)

这篇博客介绍了PAT甲级编程竞赛中的1026题——乒乓球桌分配问题,详细解析了算法思路,特别是VIP用户的优先权。文章通过实例解释了如何处理到达时间等于21:00:00、服务时间超过2小时以及VIP优先选择VIP桌等复杂情况,并提供了C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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

思路

将时间统一换算成秒。将顾客按到达时间排序,对于当前最早顾客,判断是否有空闲窗口:

  • 若窗口最早结束时间<=顾客到达时间,说明有空闲窗口。按编号逐个检查窗口结束时间,取第一个空闲窗口;若顾客是vip,则取第一个空闲窗口(包括)后的第一个空闲vip窗口(若有的话)。
  • 若窗口最早结束时间>顾客到达时间,说明无空闲窗口。需要排队等最早结束的窗口;若该窗口是vip,则结束时检查顾客后面是否有在排队的vip,若有则让vip使用;若不是vip窗口,则直接给该顾客。

注意一个重要逻辑:并不是哪个窗口最先空出来,下个人就选哪个窗口。比如:

窗口1结束时间是8:30
窗口2结束时间是8:20,最早空出来
若顾客在8:29到来,会选窗口2;
若顾客在8:31到来,会选窗口1而不是2。
在这里插入图片描述

所以选服务窗口,不是找上次结束时间最小的,而应该是找下个顾客到来时第一个空闲下来的!

测试用例:

4
08:00:00 30 1
08:10:00 10 1
08:25:00 10 1
09:00:00 10 1
3 1
2

应输出:

08:00:00 08:00:00 0
08:10:00 08:10:00 0
08:25:00 08:25:00 0
09:00:00 09:00:00 0
2 2 0

柳神的代码这个用例也是错的:1026. Table Tennis (30)-PAT甲级真题(模拟),在牛客网亦无法通过。

(2020.3.27更新)感谢SamsonKun的评论,本文代码原先在PAT测试点6无法通过,原因是在108-114行的for循环中多加了一行i++,可能是把while语句修改为for语句时忘了删掉了。修改bug后已经能通过所有测试点。

PAT:
PAT通过
牛客网:
在这里插入图片描述


坑点

到达时刻可能为21:00:00

测试点3考察的是,若到达时刻==21:00:00,则不为其提供服务。
测试用例:

1
21:00:00 1 0
2 1
1

应输出:

0 0
服务时间不得超过2小时

测试点4考察的是,若使用时间大于2小时,则按2小时计算。题中说“It is assumed that every pair of players can play for at most 2 hours”,可以被误解为已假定输入都在2小时内,坑。

vip用户在有多个空桌时优先选vip桌

测试点7考察的是,当同时有多个空桌时,vip用户会优先选vip桌子而非编号最小的桌子……题目中没有明确说明,是个坑。
测试用例:

3
08:00:00 2 0
08:01:00 1 0
08:02:00 1 1
2 1
2

应输出:

08:00:00 08:00:00 0
08:01:00 08:01:00 0
08:02:00 08:02:00 0
1 2

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Player{
   
   
    int arriveTime;
    int serveTime;
    int duration;
    int vip;
};
struct Table{
   
   
    int time;  //空闲时刻
    int sum;  //服务人数
    int vip;
};
bool 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值