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
- 分析:这道题做的真累!
- 主要是有几个条件不明。条件太多。
- they will be assigned to the available table with the smallest number.我们在安排桌子时要按照从小到达安排。安排vip桌子时也是从小开始。
- 如果没有vip桌子可用,那要用普通桌子。
- It is assumed that every pair of players can play for at most 2 hours.玩的时间最多2小时,超过2h,需要我们截取。pat_a 4号(第5个)测试点。牛客网有一个138人的测试点。
- The waiting time must be rounded up to an integer minute(s). 最后输出时间,取整(round不是直接舍去多余的小数,直接进一个么!不是向上取整么!),要四舍五入到分。pat最后一个一分的测试点。牛客网没有这个测试点。
- Notice that the output must be listed in chronological order of the serving time.输出按服务时间的先后顺序排。
- 开放时间 8:00-21:00
- 设计思路
按时间轴开始,排序后的用户(到达顺序)—>处理(1、不需要等待,直接处理 2、需要等待,要寻找结束最早的桌子,时间轴为桌子)–>如果遇到vip,校正用户,时间轴为用户。
- 主要是有几个条件不明。条件太多。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
struct _T
{
int b;
int p;
int w;
int v;
int flag;
int c;//table
};
int N;
int K;
int M;
vector<_T>in;
vector<_T>table;//只使用b,v
_T tmp_t;
int getVip(long int a,long int b)
{
for(int i=b;i<N;i++)
{
if(in.at(i).flag>0)
continue;
if(in.at(i).b<=a)
{
if(in.at(i).v==1)
return i;
}else
return -1;
}
return -1;
}
//按服务时间排序
bool isSmall(_T a,_T b)
{
if(a.b+a.w<b.b+b.w)
return true;
else if(a.b+a.w==b.b+b.w)
{
if(a.v==1&&b.v==0)
return true;
}
return false;
}
void outputT(int a)
{
int h=a/3600;
int m=(a/60)%60;
int s=a%60;
printf("%02d:%02d:%02d",
h,m,s);
}
int main()
{
cin>>N;
int h,m,s;
tmp_t.w=0;
tmp_t.flag=0;
tmp_t.c=0;
for(int i=0;i<N;i++)
{
scanf("%02d:%02d:%02d %d %d",
&h,&m,&s,&tmp_t.p,&tmp_t.v);
if(tmp_t.p>120)
tmp_t.p=120;
tmp_t.b=h*3600+m*60+s;
tmp_t.p*=60;
in.push_back(tmp_t);
}
cin>>K>>M;
tmp_t.b=8*3600;
tmp_t.v=0;
table.assign(K,tmp_t);
int v;
for(int i=0;i<M;i++)
{
cin>>v;
table.at(v-1).v=1;
}
//sort in
sort(in.begin(),in.end(),isSmall);
for(int i=0;i<N;i++)
{
if(in.at(i).flag==1)
continue;
if(in.at(i).b>=21*3600)
{
in.at(i).flag=2;
continue;
}
//get table avalible
int vj=-1;
int vjj=-1;
//+++++++++++++++++++++++++++++++++++++++++++++++++++1
for(int j=0;j<K;j++)
{
//找到不需要等待,直接可以用的桌子,从小号开始
/*
if(table.at(j).b<=in.at(i).b)//wait
{
//这样合有个问题,就是没有vip桌子,但有普通桌子时,vj会显示没有桌子可用,还是分开吧
if(in.at(i).v==1)
if(table.at(j).v==1){vj=j;break;}
else{ vj=j;break;}
}
*/
if(table.at(j).b<=in.at(i).b)//wait
{
vj=j;
break;
}
}
for(int j=0;j<K;j++)
{
if(table.at(j).b<=in.at(i).b)//wait
{
if(table.at(j).v==1)
{
vjj=j;
break;
}
}
}
if(in.at(i).v==1)
{
//有vip桌子可用,没有时就是用普通桌子
if(vjj>-1)
vj=vjj;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++2
//get min table
/*所有的table时间小于用户到达时间时vj==-1,需要选择最小的table,这是是按table时间*/
if(vj==-1)
{
vj=0;
int tmp=table.at(0).b;
for(int j=0;j<K;j++)
{
if(tmp>table.at(j).b)
{
vj=j;
tmp=table.at(j).b;
}
}
}
int vi=-1;
if(table.at(vj).b>=21*3600)
break;
//+++++++++++++++++++++++++++++++++++++++++++++++++++3
int vip_pre=getVip(table.at(vj).b,i);
//等待的人员有vip
if(vip_pre>=0)
{
//找合适的vip桌子,从小号开始
int tmp_vip=table.at(vj).b;
for(int j=0;j<K;j++)
{
if(table.at(j).v==1&&table.at(j).b<=table.at(vj).b)
{
vj=j;
break;
}
}
}
table.at(vj).c++;
if(table.at(vj).v==1)vi=vip_pre;
if(vi<=0)vi=i;
else i--;//如果是找到vip,则需要校正等待人员
in.at(vi).flag=1;
if(table.at(vj).b<in.at(vi).b)//dont wait
{
in.at(vi).w=0;
table.at(vj).b=in.at(vi).b;//更新服务时间
}else
in.at(vi).w=table.at(vj).b-in.at(vi).b;
//更新为服务结束时间
table.at(vj).b+=in.at(vi).p;
}
//按服务时间排序
sort(in.begin(),in.end(),isSmall);
for(int i=0;i<in.size();i++)
{
if(in.at(i).flag!=1)
continue;
outputT(in.at(i).b);
cout<<" ";
outputT(in.at(i).b+in.at(i).w);
//按满30进位
if(in.at(i).w%60>=30)
cout<<" "<<in.at(i).w/60+1<<endl;
else
cout<<" "<<in.at(i).w/60<<endl;
}
for(int i=0;i<K-1;i++)
cout<<table.at(i).c<<" ";
cout<<table.at(table.size()-1).c<<endl;
return 0;
}