【PAT】 A1026 Table Tennis

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

 这应该是目前接触的最难的模拟题了,这个题因为是进入时间不定的,所以更加麻烦一些。

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct Table_Tennis{
	int start_time;
	int end_time;
	int id;
	int serve_num;
	int table_clock;
	bool serve;
	bool vip;
};

struct Player{
	int start_time;
	int table_id;
	int id;
	int play_time;
	int end_time;
	int enter_time;
	int wait_time;
	bool vip;
	bool served;
};


vector < Table_Tennis > table_vec;

vector <Player> player_vec;
map < int, Player > play_map;

vector < Player > ans;
int N,K,M;
int time_S,time_F;
int clock_tennis = 0;

bool cmp(Table_Tennis &table1, Table_Tennis &table2){
	if(table1.end_time == table2.end_time){
		return (table1.id<table2.id);
	}else return (table1.end_time<table2.end_time);
}

bool cmp_id(Table_Tennis *table1, Table_Tennis *table2){
	return (table1->id<table2->id);
}

bool cmp_p(Player &player1, Player &player2){
	return (player1.enter_time<player2.enter_time);
}

bool cmp_s(Player &player1, Player &player2){
	return (player1.start_time<player2.start_time);
}
int time_converter(int h,int m, int s){
	return ((h-8)*3600+m*60+s);
}

int main(int argc, char** argv) {
	time_S = time_converter(8,0,0);
	time_F = time_converter(21,0,0);
	scanf("%d",&N);
	for (int i=0;i<N;i++){
		int h,m,s,vip,play_time;
		Player player_tmp;
		scanf("%d:%d:%d %d %d",&h,&m,&s,&play_time,&vip);
		player_tmp.play_time = play_time*60;
		if(vip>0) player_tmp.vip=true;
		else player_tmp.vip = false;
		player_tmp.enter_time = time_converter(h,m,s)-time_S;
		if (player_tmp.play_time>2*3600) player_tmp.play_time=2*3600;
		player_tmp.served=false;
		player_tmp.id = i;
		player_vec.push_back(player_tmp);
		play_map[player_tmp.enter_time] = player_tmp;
	}
	
	scanf("%d%d",&K,&M);
	
	for(int i=0;i<K;i++){
		Table_Tennis table_tmp;
		table_tmp.start_time =0;
		table_tmp.end_time =0;
		table_tmp.vip=false;
		table_tmp.id = i+1;
		table_vec.push_back(table_tmp);
	}
	for(int i=0;i<M;i++){
		int table_id;
		scanf("%d",&(table_id));
		table_vec[table_id-1].vip=true;
	}
	
	sort(player_vec.begin(),player_vec.end(),cmp_p);
	sort(table_vec.begin(),table_vec.end(),cmp);
	
	vector <Table_Tennis *> reserved_table;
	sort(table_vec.begin(),table_vec.end(),cmp);
	
	while(!play_map.empty()){
		 		
 		//int end_time = table_vec[0].end_time;
 		int end_time = clock_tennis;

 		for (int i=0;i<table_vec.size();i++){
 			if(table_vec[i].end_time <= end_time){
 				reserved_table.push_back(&(table_vec[i]));
 				//printf("clock %d reserved table %d\n",clock_tennis,table_vec[i].id); 
			}
		}
		sort(reserved_table.begin(),reserved_table.end(),cmp_id);
		for (int i=0;i<player_vec.size();i++){
			if((player_vec[i].enter_time<=clock_tennis)&&(player_vec[i].vip)){
				for (int j=0;j<reserved_table.size();j++){
					if((reserved_table[j]->vip)&&(reserved_table[j]->end_time <= end_time)&&(!player_vec[i].served)){
					//if((reserved_table[j]->end_time <= end_time)&&(!player_vec[i].served)){
						Player player_tmp = player_vec[i];
						player_tmp.start_time = clock_tennis;
						player_tmp.wait_time = round((clock_tennis-player_tmp.enter_time)/60.0);
						player_tmp.table_id  = reserved_table[j]->id;
						player_vec[i].served = true;
						play_map.erase(player_tmp.enter_time);
						//printf("vip player id %d table id %d start time %d wait time %d release time %d play_time %d \n",
						//player_tmp.id,player_tmp.table_id,player_tmp.start_time,player_tmp.wait_time,clock_tennis+player_tmp.play_time,player_tmp.play_time);
						reserved_table[j]->end_time = clock_tennis+player_tmp.play_time;
						
						if(clock_tennis<time_F) ans.push_back(player_tmp);
					}
				}				
			}
		}

		for (int i=0;i<player_vec.size();i++){
			if(player_vec[i].enter_time<=clock_tennis){
				for (int j=0;j<reserved_table.size();j++){
					if((reserved_table[j]->end_time <= end_time)&&(!player_vec[i].served)){
						Player player_tmp = player_vec[i];
						player_tmp.start_time = clock_tennis;
						player_tmp.wait_time = round((clock_tennis-player_tmp.enter_time)/60.0);
						player_tmp.table_id  = reserved_table[j]->id;
						player_vec[i].served = true;
						//printf("player id %d table id %d start time %d wait time %d release time %d\n",
						//player_tmp.id,player_tmp.table_id,player_tmp.start_time,player_tmp.wait_time,clock_tennis+player_tmp.play_time);
						play_map.erase(player_tmp.enter_time);
						reserved_table[j]->end_time = clock_tennis+player_tmp.play_time;
						
						if(clock_tennis<time_F) ans.push_back(player_tmp);
					}
				}				
			}
		}
		
		sort(table_vec.begin(),table_vec.end(),cmp);
 		
 		//int end_time = table_vec[0].end_time;
		
		map < int, Player >::iterator iter=play_map.begin();
				
		clock_tennis = max(iter->first,table_vec[0].end_time);	
		//printf("clock %d player list time %d table finish %d\n",clock_tennis,iter->first,table_vec[0].end_time);
		
		//if(iter->first > table_vec[0].end_time)	 
		//	printf ("player id %d\n",(iter->second).id);
		
		for(int i=0;i<table_vec.size();i++){
			table_vec[i].table_clock = clock_tennis;
		}
		reserved_table.clear();
		//sort(table_vec.begin(),table_vec.end(),cmp_fuck);
	}

	if(clock_tennis>time_F) break;
	sort(ans.begin(),ans.end(),cmp_s);
	
	map<int,int> table_map;
	for(int i=0; i<ans.size();i++){
		int e_h=(ans[i].enter_time+time_S)/3600 + 8;
		int e_m=((ans[i].enter_time+time_S)%3600)/60;
		int e_s=(ans[i].enter_time+time_S)%60;
		int s_h=(ans[i].start_time+time_S)/3600 + 8;
		int s_m=((ans[i].start_time+time_S)%3600)/60;
		int s_s=(ans[i].start_time+time_S)%60;
		
		if(table_map.count(ans[i].table_id)==0)  table_map[ans[i].table_id]=1;
		else table_map[ans[i].table_id]++;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",e_h,e_m,e_s,s_h,s_m,s_s,ans[i].wait_time);
	}
	
	for(int i=1;i<=K;i++){
		if(table_map.count(i))
		printf("%d",table_map[i]);
		else printf("0");
		
		if (i<K) printf(" ");
		else printf("\n");		
	}
		
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值