PAT 1026. Table Tennis (30) 【26/30】

7 篇文章 0 订阅

1026. Table Tennis (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码


本题拿了26分,还有两个地方存在疑问,日后再探讨。


1、若VIP桌空闲,VIP桌也可接待ordinary people。

2、大于等于21:00:00都不能被服务
3、四舍五入:大于等于0.5啊!!!
4、注意超过2小时的不是不要这组数据,而是截断至7200秒

5、VIP会员来时,若有多个空闲,选最小的VIP桌,真没读出来。。。(很疑惑,没包含

6、保证客户到达时间在08:00:00到21:00:00之间,加以控制很疑惑,题目明明已限定


思路分析:(3重判断)



对人到达的时间排个序,然后依次处理每个人。对于第i个人,

if如果当前桌子有空闲,不管是不是VIP桌,则直接使用,因为有空闲不可能有人排队,更不可能有VIP在后面;

else{如果当前桌子无空闲,那么找出第一个最早结束使用的桌子,

            if{ 若该桌子是VIP桌,则看截止该桌子结束使用的时间有没有VIP在排队 且 没被处理,

                            if 如果有会员,则优先处理该VIP,然后进行新一轮的重复扫描,给该人找到合适的桌子,

                            else 如果无会员,则把该人安排在这张VIP桌。

            }else 若该桌子非VIP桌,则马上安排该人在此桌。

}



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=10010,INF=0x7fffffff;
int tabTime[110],mark[110],cnt[110];//每个球桌的当前最后时间
struct E{
	int arrive;  //到达的时间
	int process; //需要服务的时间
	int isVip;
	int bServe;   //开始服务的时间
	int isServed; //因为后面的VIP可能先被服务
}r[MAX];
bool cmp(E a,E b)
{
	return a.arrive<b.arrive;
}
bool cmp1(E a,E b)
{
	return a.bServe<b.bServe;
}

int main()
{	
    //freopen("G:\\in.txt","r",stdin);
	
	int i,j,s,hour,min,sec,service,isV,n,k,m;//n人数,k桌子数,m-VIP桌子数
	while(scanf("%d",&n)!=EOF){
		for(i=0;i<n;i++){
			scanf("%d%*c%d%*c%d %d %d",&hour,&min,&sec,&service,&isV);
			r[i].arrive=3600*hour+60*min+sec;
			r[i].process=service*60;
			if(r[i].process>2*3600) //超过2小时的要截断至2小时,从存储根源上解决超时*********2分
				r[i].process=2*3600;
			r[i].isVip=isV;
			r[i].bServe=INF; //初始化
			r[i].isServed=0; //初始化
		}
		scanf("%d%d",&k,&m);
		memset(mark,0,sizeof(mark));
		memset(cnt,0,sizeof(cnt));
		for(i=0;i<m;i++){
			int tmp;
			scanf("%d",&tmp);
			mark[tmp-1]=1;  //进行转化,假设桌子从0开始编号。
		}
//------------ 针对每个人开始处理-------
		sort(r,r+n,cmp);
		for(i=0;i<k;i++)  //初始化 以秒为单位
			tabTime[i]=8*3600; 
		for(i=0;i<n;i++){
			if(r[i].isServed==0){
				int minTime=INF,id;
				for(j=0;j<k;j++){
					if(r[i].arrive>tabTime[j]){  //如果有桌子空闲.........................
						r[i].bServe=r[i].arrive;
						r[i].isServed=1;

						tabTime[j]=r[i].arrive+r[i].process;
						if(r[i].bServe<21*3600)
							cnt[j]++;
						id=-1;
						break;
					}
					else{
						if(tabTime[j]<minTime){
							minTime=tabTime[j];
							id=j;
						}
					}
				}
				if(id!=-1){
					if(mark[id]==1){  //如果是VIP桌子
						int isThere=0;
						for(s=i;s<n&&r[s].arrive<=tabTime[id];s++){
							if(r[s].isVip==1&&r[s].isServed==0){ //有VIP且没被服务过
								r[s].bServe=tabTime[id];
								r[s].isServed=1;
								tabTime[id]=tabTime[id]+r[s].process;
								if(r[s].bServe<21*3600)
									cnt[id]++;
								isThere=1;
								break;
							}
						}
						if(isThere==1)
							i--;  //与本轮结束后i的自增相抵消,下一轮重新给第i个人找桌子。
						else{
							r[i].bServe=tabTime[id];
							r[i].isServed=1;
							tabTime[id]=tabTime[id]+r[i].process;
							if(r[i].bServe<21*3600)
								cnt[id]++;
						}
					}
					else{
						r[i].bServe=tabTime[id];
						r[i].isServed=1;
						tabTime[id]=tabTime[id]+r[i].process;
						if(r[i].bServe<21*3600)
							cnt[id]++;
					}
				}
			}
		}
		sort(r,r+n,cmp1);
		for(i=0;i<n;i++){
			if(r[i].bServe<21*3600){
			   printf("%02d:%02d:%02d ",r[i].arrive/3600,(r[i].arrive%3600)/60,(r[i].arrive%3600)%60);
			   printf("%02d:%02d:%02d ",r[i].bServe/3600,(r[i].bServe%3600)/60,(r[i].bServe%3600)%60);
//------------------四舍五入-----------------------*********************************************2分
				float t1;
				int ans,t2;
				t1=(float)(r[i].bServe-r[i].arrive)/60;
				t2=(r[i].bServe-r[i].arrive)/60;
				if(t1-t2>=0.5)  //四舍五入。
					ans=t2+1;
				else
					ans=t2;
				printf("%d\n",ans);
			}
		}
		for(i=0;i<k;i++){
			if(i!=0) printf(" ");
			printf("%d",cnt[i]);
		}
		printf("\n");
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值