pat 1026

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
 
 
//这道题做了一天啊,终于过了。参考了其他人的答案。
//把几种情况想清楚了。在等待队列中找到vip会员时,把vip会员放到当前循环到的位置,其余人的位置依次往后移动一个
#include <stdio.h>
#include <algorithm>
using namespace std;

int n, k, m;

struct Person
{
	int arriveTime;
	int service;
	int wait;
	int tag;
	int beginTime;
} psn[10010];

struct Table
{
	int endTime;
	int num, tag;
} tbe[110];

bool cmp(Person a, Person b)
{
	return a.arriveTime < b.arriveTime;
}

bool cmp1(Person a, Person b)
{
	return a.beginTime < b.beginTime;
}
int findIndex(int time)
{
	int i = 0;

	for(i = 1; i <= k; i++)
	{
		if(tbe[i].endTime <= time)
			return i;
	}

	return -1;
}

int findIndexVip(int time)
{
	int i;

	for(i = 1; i <= k; i++)
	{
		if(tbe[i].endTime <= time && tbe[i].tag == 1)
			return i;
	}

	return -1;
}

int getEarlyTime()
{
	int time = 21 * 3600, i, index = -1;

	for(i = 1; i <= k; i++)
	{
		if(tbe[i].endTime < time)
		{
			time = tbe[i].endTime;
			index = i;
		}
	}

	return index;
}

int main()
{
	int i, j, hour, minute, second, tmp, index, indexVip;
	Person pp;

	while(scanf("%d", &n) != EOF)
	{
		for(i = 0; i < n; i++)
		{
			scanf("%d:%d:%d", &hour, &minute, &second);
			psn[i].arriveTime = hour * 3600 + minute * 60 + second;
			scanf("%d%d", &psn[i].service, &psn[i].tag);
			if(psn[i].service >= 120)
				psn[i].service = 120;
			psn[i].service = psn[i].service * 60;
		}

		sort(psn, psn + n, cmp);

		scanf("%d%d", &k, &m);

		for(i = 1; i <= k; i++)
		{
			tbe[i].endTime = 8 * 3600;
			tbe[i].num = 0;
			tbe[i].tag = 0;
		}

		for(i = 1; i <= m; i++)
		{
			scanf("%d", &tmp);
			tbe[tmp].tag = 1;
		}

		for(i = 0; i < n; i++) 
		{
			if(psn[i].arriveTime >= 21 * 60 * 60)
				break;
			
			index = findIndex(psn[i].arriveTime);  //如果该人到达的时候有空闲桌子
			indexVip = findIndexVip(psn[i].arriveTime); //如果该人到达的时候有空闲的vip桌子

			if(index > 0)
			{
				if(psn[i].tag == 1 && indexVip > 0)
				{
					tbe[indexVip].num++;
					tbe[indexVip].endTime = psn[i].arriveTime + psn[i].service;

					printf("%02d:%02d:%02d", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
					printf(" %02d:%02d:%02d ", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
					printf("0\n");
				}
				else
				{
					tbe[index].num++;
					tbe[index].endTime = psn[i].arriveTime + psn[i].service;

					printf("%02d:%02d:%02d", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
					printf(" %02d:%02d:%02d ", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
					printf("0\n");
				}
			}
			else
			{
				index = getEarlyTime();

				if(index == -1) break;

				if(tbe[index].tag == 0) //如果该桌子不是vip桌子
				{
					tbe[index].num++;
					psn[i].beginTime = tbe[index].endTime;
					psn[i].wait = tbe[index].endTime - psn[i].arriveTime;
					tbe[index].endTime = psn[i].beginTime + psn[i].service;
					
					printf("%02d:%02d:%02d", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
					printf(" %02d:%02d:%02d ", psn[i].beginTime/3600, psn[i].beginTime%3600/60, psn[i].beginTime%3600%60);
					printf("%d\n", (psn[i].wait + 30)/60);
				}
				else 
				{
					if(psn[i].tag == 1) // 当前的人正好是vip会员
					{
						tbe[index].num++;
						psn[i].beginTime = tbe[index].endTime;
						psn[i].wait = tbe[index].endTime - psn[i].arriveTime;
						tbe[index].endTime = psn[i].beginTime + psn[i].service;

						printf("%02d:%02d:%02d", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
						printf(" %02d:%02d:%02d ", psn[i].beginTime/3600, psn[i].beginTime%3600/60, psn[i].beginTime%3600%60);
						printf("%d\n", (psn[i].wait + 30)/60);
					}
					else//在等待队列中找一个vip会员并将该vip会员的位置放到当前位置i,其余人的位置依次往后移动一个
					{
						for(j = i + 1; j <= n; j++)
						{
							if(psn[j].arriveTime <= tbe[index].endTime && psn[j].tag == 1)
								break;
						}

						if(j <= n)
						{
							pp = psn[j];

							for(j = j - 1; j >= i; j--)
								psn[j+1] = psn[j];

							psn[i] = pp;
						}

						tbe[index].num++;
						psn[i].beginTime = tbe[index].endTime;
						psn[i].wait = tbe[index].endTime - psn[i].arriveTime;
						tbe[index].endTime = psn[i].beginTime + psn[i].service;

						printf("%02d:%02d:%02d", psn[i].arriveTime/3600, psn[i].arriveTime%3600/60, psn[i].arriveTime%3600%60);
						printf(" %02d:%02d:%02d ", psn[i].beginTime/3600, psn[i].beginTime%3600/60, psn[i].beginTime%3600%60);
						printf("%d\n", (psn[i].wait + 30)/60);
					}//else
				}//else
			}//else
		}//for

		for(i = 1; i <= k; i++)
		{
			if(i != k)
				printf("%d ", tbe[i].num);
			else
				printf("%d\n", tbe[i].num);
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值