PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
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桌子 ::
::
::
:: 答案为:
:: ::
:: ::
:: ::
:: :: //边缘测试例子 :: 答案为: //超过两小时的例子 ::
:: 答案为:
:: ::
:: :: //关于四舍五入为1分钟的例子,大约等于30秒才+1分钟,小于30则不+ ::
::
:: 答案为: :: ::
:: ::
:: ::
AC代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int a;//到达的时间
int s;//开始的时间
int t;//服务时间 };
struct cmp
{
bool operator()(node &x,node &y)
{
return x.a > y.a;
}
};
struct cmp2
{
bool operator()(node &x,node &y)
{
return x.s > y.s;
}
};
priority_queue<node ,vector<node>,cmp>a,b;//a为普通用户,b为vip用户
priority_queue<node ,vector<node>,cmp2>ans;
int n,m,k;
int tab[];//桌子服务的人数
int vt[];//标记是不是vip桌子
node q[];//正在服务的人们
int main(){
cin>>n;
memset(vt,,sizeof(vt));
memset(tab,,sizeof(tab));
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
while(!ans.empty()) ans.pop();
for(int i=;i<=n;i++){
int hh,mm,ss;
node x;
scanf("%d:%d:%d",&hh,&mm,&ss);
hh=hh*+mm*+ss;
cin>>mm;
mm*=;
if(mm>*) mm=*;
x.a=hh;
x.t=mm;
int v;
cin>>v;
if(x.a>=*){//时间超过来的直接不要
continue;
}
if(v==){
a.push(x);
}else{//vip
b.push(x);
}
}
cin>>k>>m;
for(int i=;i<=k;i++){//初始化
q[i].a=-;//初始来的人时间为-1
q[i].s=*;
q[i].t=;
}
for(int i=;i<=m;i++){
int x;
cin>>x;
vt[x]=;
}
while(!a.empty() || !b.empty())//开始执行
{
int min_t=;//找到一个最早的结束时间
int min_k=-;//最早结束时间对于的桌号
for(int i=;i<=k;i++){
if(min_t>q[i].s+q[i].t){
min_t=q[i].s+q[i].t;
min_k=i;
}
}
if(min_t>=*){
break;
}
//【1】先检查是不是vip桌子
if(vt[min_k]){
if(!b.empty()){//先看看队列里有没有vip
node x=b.top();
if(x.a<min_t){//如果vip在队列里,优先考虑
ans.push(q[min_k]);//把排在min_k的人放进最终答案中
q[min_k].a=x.a;
q[min_k].t=x.t;
q[min_k].s=min_t;
tab[min_k]++;//对于桌子服务的人数++
b.pop();
continue;
}
}
}
//【2】不是vip桌子
node x,y;
if(!a.empty()){
x=a.top();
}else{//小技巧,空的话就99999999
x.a=;
}
if(!b.empty()){
y=b.top();
}else{
y.a=;
}
if(x.a<y.a){//两者谁先就放谁
ans.push(q[min_k]);
tab[min_k]++;
q[min_k].a=x.a;
q[min_k].t=x.t;
a.pop();
if(x.a<min_t){
q[min_k].s=min_t;
}else{
q[min_k].s=x.a;
}
}else{//如果是vip早的话,看看有没有同样结束的vip桌子
//(1)坑点!优先把vip分到编号小的vip桌子,而不是编号小的桌子
int flag=;
for(int j=;j<=k;j++){
if(vt[j] && y.a >= q[j].s+q[j].t){//有符合条件的桌子
ans.push(q[j]);
tab[j]++;
q[j].a=y.a;
q[j].t=y.t;
q[j].s=y.a;
b.pop();
flag=;
break;
}
}
if(flag) continue;
//(2) 否则,就放在min_k这张桌子上
ans.push(q[min_k]);
tab[min_k]++;
q[min_k].a=y.a;
q[min_k].t=y.t;
b.pop();
if(y.a<min_t){
q[min_k].s=min_t;
}else{
q[min_k].s=y.a;
}
}
}
for(int i=;i<=k;i++){
ans.push(q[i]);
}
//输出
while(!ans.empty()){
node x = ans.top();
ans.pop(); if(x.a==-){//一开始是默认没有人的
continue;
}
int cha=x.s-x.a;//计算时间差 int hh=x.a/;
x.a=x.a%;
int mm=x.a/;
int ss=x.a%;
printf("%02d:%02d:%02d ",hh,mm,ss); hh=x.s/;
x.s=x.s%;
mm=x.s/;
ss=x.s%;
printf("%02d:%02d:%02d ",hh,mm,ss); cout<<int(cha*1.0/+0.5)<<endl;//四舍五入,不是向上取整
}
for(int i=;i<=k;i++){
cout<<tab[i];
if(i!=k) cout<<" ";
}
return ;
}

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. Fo ...
- PAT甲级1026. Table Tennis
PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...
- PAT 甲级 1026 Table Tennis(模拟)
1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...
- 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 ...
- 【PAT甲级】1026 Table Tennis (30 分)(结构体排序,trick较多)
题意: 输入一个正整数N(<=10000),表示客户(对)的大小,接着输入N行数据,每行包括一对顾客到场的时间,想要玩的时间,以及是否是VIP客户.接下来输入两个正整数K,M(K<=100 ...
- PAT甲级1026 Table Tennis【模拟好题】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560 题意: 有k张乒乓球桌,有的是vip桌 ...
- 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 ...
- 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 ...
- PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
随机推荐
- linux(centos)下安装supervisor进程管理工具
在接触supervisor进程管理工具之前,使用springboot打包部署到linux服务器的流程是这样子的,如下图所示: 上图展示的就是最一般的流程,如果项目是小项目或者demo可以这样子去部署, ...
- 2018-2019 XIX Open Cup, Grand Prix of Korea B - Dev, Please Add This!
B - Dev, Please Add This! 思路: 对于每一个经过 '*' 的横线和竖线看成一个整体,设他们分别为分量x和分量y 用2-sat考虑这个问题, 如果要经过 '*' ,那么x和y至 ...
- Java基础 TreeSet()来实现数组的【定制排序】 : Comparable接口(自然排序) 或者 Comparator接口 (定制排序)
笔记: //排序真麻烦!没有C++里的好用又方便!ORZ!ORZ!数组排序还还自己写个TreeSet()和( Comparable接口(自然排序) 或者 Comparator接口 (定制排序))imp ...
- java基础:多态过程中的动态绑定
重刷java-core的chapter05,P158 重读多态,感觉又不一样了. 记录一下对象方法执行过程: 1. 编译器查看对象声明类型和方法名,如class.fuction(param),cla ...
- C语言——for循环和while循环的效率区别——类似哨兵思想
int ID_Conv_Sentinel(int u16device_cfg_num) { int i8id; int size=0; int i=0; size = sizeof(Device_ID ...
- VS 项目清理小工具 ClearSolution
简介 VS项目清理小工具,通过INI配置文件快速清理项目,清除无用的数据库等文件与文件夹,简单实用. 支持平台 Windows 开源许可 ClearSolution 遵循 [Apache 协议],使用 ...
- 浅析pagehelper分页原理(转)
之前项目一直使用的是普元框架,最近公司项目搭建了新框架,主要是由公司的大佬搭建的,以springboot为基础.为了多学习点东西,我也模仿他搭了一套自己的框架,但是在完成分页功能的时候,确遇到了问题. ...
- Appium自动化测试教程-自学网-app基础知识
Instrumentation的缺点是不支持跨应用,比如我想要先调起通讯录,在操作其他的app,则不支持. 第一步,应该确定系统哪些模块适合自动化.哪些不适合做自动化,明确做自动化给我们带来的好处是什 ...
- bzoj3508: 开灯
题目链接 题解 设\(b[i]=a[i]\ xor\ a[i+1]\) 我们可以发现,修改只会改变\(b[l-1]\)和\(b[r]\) 然后发现\(b[i]=1\)的点最多\(2*k\)个 状压\( ...
- wait系列
转自 http://blog.csdn.net/todd911/article/details/15028511 1.wait函数和waitpid函数 当一个进程正常或异常终止时,内核就向其父进程发送 ...