PAT甲级1026 Table Tennis

1026 Table Tennis (30分)

测试点

  • 测试点1,2: 没有VIP球桌空闲时,VIP球员正常排队;
  • 测试点4: 球员打球时间超过两小时的,都按照两小时计算;
  • 测试点5,7: VIP球员在普通球桌和VIP球桌同时空闲时,会优先选择编号较小的VIP球桌;
  • 测试点8: 球员等待时间从秒转换为分钟时按照四舍五入计算;
  • 球桌的编号为1~N;

思路:和前面的银行排队题一样,进行简单的模拟,不过我写的好长呀=。=模拟的过程有些冗余了

#include
using namespace std;

struct player{
	int hour,min,sec;
	int arrivetime,playingtime
	int isvip;
	int servetime=21*60*60+1,endtime;
};
bool cmp(player a,player b)
	return a.arrivetime<b.arrivetime;
bool cmp2(player a,player b)
	return a.servetime<b.servetime;

int main(){
	int n;
	cin>>n;
	vector<player> players(n);
	for(int i=0;i<n;i++){
		scanf("%d:%d:%d",&players[i].hour,&players[i].min,&players[i].sec);
		players[i].arrivetime=players[i].sec+players[i].min*60+players[i].hour*60*60;
		cin>>players[i].playingtime;
		players[i].playingtime*=60;
		if(players[i].playingtime>120*60)players[i].playingtime=120*60;
		cin>>players[i].isvip;
	}
	int k,m;
	cin>>k>>m;
	vector<int> vip_table(k,0);
	vector<int> tables(k,0);
	for(int i=0;i<m;i++){
		int tmp;cin>>tmp;
		vip_table[tmp-1]=1;
	}
	
	sort(players.begin(),players.end(),cmp);
	vector<vector<int>> tabs(k);
	vector<int> waitingline;
	vector<int> vipline;
	int j=0;
	for(int t=8*60*60;t<21*60*60;t++){
		if(j<n && t==players[j].arrivetime){
			waitingline.push_back(j);
			if(players[j].isvip==1)vipline.push_back(j);
			j++;
		}
		if(vipline.size()!=0 && waitingline[0]==vipline[0])
			for(int i=0;i<k;i++)
				if(vip_table[i]==1)
					if(tabs[i].size()==0 || players[tabs[i][0]].endtime==t){
						if(tabs[i].size()!=0)tabs[i].erase(tabs[i].begin());
						tables[i]+=1;
						tabs[i].push_back(vipline[0]);
						vector<int>::iterator it=find(waitingline.begin(),waitingline.end(),vipline[0]);
						waitingline.erase(it);
						players[vipline[0]].servetime=t;
						players[vipline[0]].endtime=t+players[vipline[0]].playingtime;
						vipline.erase(vipline.begin());
					}
		for(int i=0;i<k;i++){
			if(tabs[i].size()==0 || players[tabs[i][0]].endtime==t){
				if(tabs[i].size()!=0)tabs[i].erase(tabs[i].begin());
				if (waitingline.size()!=0){
					tables[i]+=1;
					if(vip_table[i]==0 || vipline.size()==0){
						tabs[i].push_back(waitingline[0]);
						players[waitingline[0]].servetime=t;
						players[waitingline[0]].endtime=t+players[waitingline[0]].playingtime;
						if(vipline.size()!=0 && waitingline[0]==vipline[0])vipline.erase(vipline.begin());
						waitingline.erase(waitingline.begin());
					}
					else{
						tabs[i].push_back(vipline[0]);
						vector<int>::iterator it=find(waitingline.begin(),waitingline.end(),vipline[0]);
						waitingline.erase(it);
						players[vipline[0]].servetime=t;
						players[vipline[0]].endtime=t+players[vipline[0]].playingtime;
						vipline.erase(vipline.begin());
					}
				}	
			}
		}
	}
	sort(players.begin(),players.end(),cmp2);
	for(int i=0;i<n;i++){
		if(players[i].servetime>21*60*60)break;
		printf("%02d:%02d:%02d",players[i].hour,players[i].min,players[i].sec);
		int h=players[i].servetime/3600;
		int m=(players[i].servetime-h*3600)/60;
		int s=(players[i].servetime-h*3600-m*60);
		printf(" %02d:%02d:%02d",h,m,s);
		int waitingtime=round((players[i].servetime-players[i].arrivetime)/60.0);
		cout<<" "<<waitingtime<<endl;
	}
	for(int i=0;i<k-1;i++)cout<<tables[i]<<" ";
	cout<<tables[k-1];
	return 0;
} 

你可能感兴趣的:(PAT甲级刷题历程)