1026 Table Tennis -PAT甲级

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

这道题的模拟过程很复杂,参考的思路以及代码为https://blog.csdn.net/richenyunqi/article/details/79562434

思路:建立运动员、球桌以及球桌与运动员联合的结构体,在输入数据的时候的下标默认不改变,以后用下标来进行操作

建立一个优先级队列,用来存储桌子是否被占用以及队员到达的时间或者桌子空闲的时间

建立一个等待的链表,方便删除元素,存储等待中的队员

满分代码如下:

 

#include<bits/stdc++.h>
using namespace std;
int n,k,m;
struct Player{
	int arrive=0,play=0,vip=0;
	Player(){}
	Player(int aa,int pp,int vv){
		arrive=aa;
		play=pp;
		vip=vv;
	}
}player[10005];
struct Table{
	int flag=0,vip=0,num=0;//是否被占用,是否为vip,一天内服务的数量 
	Table(){}
	Table(int ff,int vv,int nn){
		flag=ff;
		vip=vv;
		num=nn;
	}
}table[105];
struct PlayerTable{
	int tableNum,playerNum,time;//球台编号、运动员编号、服务时间
	PlayerTable(){}
	PlayerTable(int tt,int pp,int ti){
		tableNum=tt;
		playerNum=pp;
		time=ti;
	} 
	bool operator < (const PlayerTable &a)const{
		return time>a.time;
	}
};
priority_queue<PlayerTable>playertable;//优先级队列
list<int>waitplayer;//等待队列
const int closetime=21*3600;//体育馆关闭时间
int searchTable(bool vip){
	//查找空闲的球台,如果运动员是vip,优先查找vip球台。
	//查找到返回该球台的索引,否则返回-1
	if(vip){
		for(int i=1;i<=k;i++){
			if(!table[i].flag&&table[i].vip){
				return i;
			}
		}
	}
	for(int i=1;i<=k;i++){
		if(!table[i].flag){
			return i;
		}
	} 
	return -1;
} 
void output(int t1,int t2){
	//输出到达时间t1,接受服务时间t2,两者之间的时间间隔
	//注意这里以分为单位,且需要四舍五入
	printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",t1/3600,t1/60%60,t1%60,t2/3600,t2/60%60,t2%60,(t2-t1+30)/60); 
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int h,m,s,play,vip;
		scanf("%d:%d:%d%d%d",&h,&m,&s,&play,&vip);
		play=min(play,120);
		play*=60;
		player[i]={(h*60+m)*60+s,play,vip};
		playertable.push({0,i,(h*60+m)*60+s});
	}
	scanf("%d%d",&k,&m);
	for(int i=0;i<m;i++){
		int a;
		scanf("%d",&a);
		table[a].vip=1;
	}
	while(!playertable.empty()){
		PlayerTable p = playertable.top();//获取队首元素 
		playertable.pop();//弹出队首元素 
		if(p.time>=closetime)
			break;
		if(p.tableNum==0){
			//球台编号为0,表示需要分配球台
			int index=searchTable(player[p.playerNum].vip);
			if(index!=-1){
				//找到合适的球台
				table[index].flag=1;//该球台被占用
				int endTime=p.time+player[p.playerNum].play;
				//获取该球台的空闲时间压入栈
				playertable.push(PlayerTable(index,p.playerNum,endTime));
				output(player[p.playerNum].arrive,p.time);
				table[index].num++;//递增该球台服务的人数 
			}else{
				//查找不到空闲球台加入等待队列
				waitplayer.push_back(p.playerNum); 
			}
		}else{
			//表示该球台空
			if(waitplayer.empty()){
				table[p.tableNum].flag=0;//闲置 
			}else{
				int temp;
				//等待序列不为空
				if(!table[p.tableNum].vip){
					//该球台非vip
					 temp=waitplayer.front();
					 waitplayer.pop_front();
				}else{
					//该球台是vip
					auto i=waitplayer.begin();
					while(i!=waitplayer.end()&&!player[*i].vip)
						++i;
					//查找等待序列中的第一个vip
					if(i==waitplayer.end()){
						//没有vip
						temp=waitplayer.front();
						waitplayer.pop_front(); 
					}else{
						//有
						temp=*i;
						waitplayer.erase(i); 
					}
				}
				int endTime=p.time+player[temp].play;
				playertable.push({p.tableNum,temp,endTime});
				output(player[temp].arrive,p.time);
				table[p.tableNum].num++;
				
			}
		}
	}
	for(int i=1;i<=k;i++){
		if(i!=1) printf(" ");
		printf("%d",table[i].num);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值