zoukankan      html  css  js  c++  java
  • 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

    题目大意:说是有k张球桌,从08:00到21:00是开放时间。有N组玩家,各自来到场地的时间不同,每组规定可以用球桌的时间是小于2个小时。题目还规定有些组玩家是VIP,他
    们有权利优先使用VIP的球桌。根据所给的玩家arrive时间,service时间,来求解玩家需要等待的时间,及其开始玩的时间。
    初看下题目,写了些整体的数据构成,利用struct来分别存储player,table。但结合上VIP的情况时,就不知道该怎么入手,网上搜了下解题方法,自己再敲一遍。
    主要对模拟的情况分不太懂。需要继续努力。这里附上参考别人所写的代码。

    #include<iostream> 
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define max_player 10002
    #define max_table 102
    #define INF 0x6FFFFFFF 
    struct Player{
    	int arrive_time;
    	int start_time;
    	int serve;
    	int wait;
    	int vip;
    };
    Player player[max_player];
    struct Table{
    	int vip;
    	int serve_end_time;
    	int num;
    };
    Table table[max_table];
    int n,m,k;
    bool cmp(Player p1,Player p2){
    	return p1.arrive_time<p2.arrive_time;
    }
    bool cmp2(Player p1,Player p2){
    	if(p1.start_time<p2.start_time){
    		return 1;
    	}else if(p1.start_time == p2.start_time && p1.arrive_time<p2.arrive_time){
    		return 1;
    	}else {
    		return 0;
    	}
    }
    void update(int p, int t){
    	//cout<<p<<" "<<t<<" "<<endl;
    	//cout<<p<<"  "<<t<<endl;
    	player[p].start_time=player[p].arrive_time>table[t].serve_end_time?player[p].arrive_time:table[t].serve_end_time;
    	player[p].wait=player[p].start_time-player[p].arrive_time;
    	table[t].num++;
    	table[t].serve_end_time=player[p].start_time+(player[p].serve<7200?player[p].serve:7200);
    }
    int main(){
    	scanf("%d",&n);
    	int h,mm,s,serve,vip;
    	int i,j;
    	for(i=0;i<n;i++){
    		scanf("%d:%d:%d %d %d",&h,&mm,&s,&serve,&vip);
    		player[i].arrive_time = h*3600+mm*60+s;
    		player[i].vip = vip;
    		player[i].serve = serve*60;
    		player[i].start_time = INF;
    		player[i].wait = INF;
    	}
    	scanf("%d %d",&k,&m);
    	for(j=0;j<k;j++){
    		table[j].vip=0;
    		table[j].serve_end_time=8*3600;
    		table[j].num = 0;
    	}
    	int vip_table;
    	for(j=0;j<m;j++){
    		scanf("%d",&vip_table);
    		table[vip_table-1].vip=1;
    	}
    	sort(player,player+n,cmp);
    	
    	for(i=0;i<n;i++){
    		if(player[i].start_time != INF) continue;
    		int min_table_time=INF;
    		//cout<<i<<endl;
    		for(j=0;j<k;j++){
    			if(min_table_time > table[j].serve_end_time){
    				min_table_time = table[j].serve_end_time;
    				//cout<<j<<"h"<<endl;
    			}
    		}
    		
    		int last_start_time = min_table_time>player[i].arrive_time?min_table_time:player[i].arrive_time;
    		//cout<<last_start_time<<"   "<<player[i].arrive_time<<endl;
    		
    		if(last_start_time >= 21*3600)break;
    		//cout<<last_start_time<<endl;
    		vector<int>cur_player;
    		vector<int>cur_table;
    		for(j=i;j<n;j++){
    			if(player[j].start_time==INF&&player[j].arrive_time<=last_start_time){
    				cur_player.push_back(j);
    				//cout<<j<<endl;
    			}
    		}
    		//cout<<last_start_time<<endl;
    		for(j=0;j<k;j++){
    			if(table[j].serve_end_time<=last_start_time){
    				cur_table.push_back(j);
    				//cout<<j<<endl;
    			}
    		}
    		int flag = 0;
    		if(cur_player.size()==1&&cur_table.size()>1){
    			if(player[cur_player[0]].vip == 1){
    				int sum = cur_table.size();
    				for(int t = 0;t<sum;t++){
    					if(table[cur_table[t]].vip == 1){
    						flag = 1;
    						update(cur_player[0],cur_table[t]);
    						break;
    					}
    				}
    			}
    		}
    		else if(cur_table.size() == 1 && cur_player.size() >1){
    			if(table[cur_table[0]].vip==1){
    				int sum = cur_player.size();
    				for(int t = 0;t<sum;t++){
    					if(player[cur_player[t]].vip == 1){
    						flag = 1;
    						update(cur_player[t],cur_table[0]);
    						break;
    					}
    				}
    			}
    		}
    		else if(cur_table.size() > 1 && cur_player.size() >1){
    			for(int t = 0;t<cur_table.size();t++){
    				if(table[cur_table[t]].vip == 1){
    					for(int tt = 0;tt<cur_player.size();tt++){
    						if(player[cur_player[tt]].vip == 1){
    							flag = 1;
    							update(cur_player[tt],cur_table[t]);
    							break;
    						}
    					}
    				}
    			}
    		}
    		if(flag == 0){
    			update(cur_player[0],cur_table[0]);
    		}
    		--i;//可能第i组的player没有被安排,所以对第i组进行判断。 
    	}
    	sort(player,player+n,cmp2);
    	int h1,m1,s1,h2,m2,s2;
    	//cout<<player[0].start_time<<" "<<player[1].start_time<<endl;
    	for(i=0;i<n;i++){
    		if(player[i].start_time >= 21*3600)break;
    		int num=player[i].arrive_time;
    		h1 = num/3600;
    		num=num%3600;
    		m1 = num/60;
    		num=num%60;
    		s1=num;
    		
    		num = player[i].start_time;
    		h2 = num/3600;
    		num=num%3600;
    		m2=num/60;
    		num=num%60;
    		s2 = num;
    		printf("%02d:%02d:%02d %02d:%02d:%02d %d
    ",h1,m1,s1,h2,m2,s2,(player[i].wait+30)/60);
    	}
    	for(i=0;i<k;i++){
    		if(i+1!=k){
    			printf("%d ",table[i].num);
    		}else{
    			printf("%d
    ",table[i].num);
    		}
    	}
    	return 0;
    }
    

      





  • 相关阅读:
    debian系统完全卸载mysql
    已解决:Linux虚拟机创建后无法切换到root
    已解决:win10 下MarkdownPad2渲染出错:This View has crashed!
    计算机网络---运输层
    计算机网络---网络层
    计算机网络---数据链路层
    计算机网络---概述
    计算机网络---物理层
    go的命令行参数---flag
    Go---第九章:使用共享变量实现并发(小知识点笔记)
  • 原文地址:https://www.cnblogs.com/grglym/p/7686198.html
Copyright © 2011-2022 走看看