PAT甲级1026

1026. Table Tennis (30)

时间限制
400 ms
内存限制
65536 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

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int K = 111;//窗口数
const int INF = 1000000000;//无穷大
struct Player
{
	int arriveTime, startTime, trainTime;//到达时间、训练开始时间及训练时长
	bool isVIP;//是否是VIP球员
}newPlayer;//临时存放新读入的球员
struct Table
{
	int endTime, numServe;//当前占用该球桌的球员的结束时间及已训练的人数
	bool isVIP;//是否是VIP球桌
}table[K];//K个球桌
vector<Player> player;//球员队列
int convertTime(int h, int m, int s)
{
	return h * 3600 + m * 60 + s;//将时间转换为以s为单位,方便比较和计算
}
bool cmpArriveTime(Player a, Player b)
{
	return a.arriveTime < b.arriveTime;//按到达时间排序
}
bool cmpStartTime(Player a, Player b)
{
	return a.startTime < b.startTime;//按开始时间排序
}
//编号VIP从当前VIP球员移到下一个VIP球员
int nextVIPPlayer(int VIPi)
{
	VIPi++;//先将VIPi加1
	while (VIPi < player.size() && player[VIPi].isVIP == 0)
	{
		VIPi++;//只要当前球员不是VIP,就让VIPi后移一位
	}
	return VIPi;//返回下一个VIP球员的ID
}
//将编号为tID的球桌分配给编号为pID的球员
void allotTable(int pID, int tID)
{
	if (player[pID].arriveTime <= table[tID].endTime)//更新球员的开始时间
	{
		player[pID].startTime = table[tID].endTime;
	}
	else
	{
		player[pID].startTime = player[pID].arriveTime;
	}
	//该球桌的训练结束时间更新为新球员的结束时间,并让服务人数加1
	table[tID].endTime = player[pID].startTime + player[pID].trainTime;
	table[tID].numServe++;
}
int main()
{
	int n, k, m, VIPtable;
	scanf("%d", &n);//球员数
	int stTime = convertTime(8, 0, 0);//开门时间为8点
	int edTime = convertTime(21, 0, 0);//关门时间为21点
	for (int i = 0; i < n; i++)
	{
		int h, m, s, trainTime, isVIP;//时、分、秒、训练时长、是否是VIP球员
		scanf("%d:%d:%d %d %d", &h, &m, &s, &trainTime, &isVIP);
		newPlayer.arriveTime = convertTime(h, m, s);//到达时间
		newPlayer.startTime = edTime;//开始时间初始化为21点
		if (newPlayer.arriveTime >= edTime) continue;//21点及以后的直接排除
		//训练时长
		newPlayer.trainTime = trainTime <= 120 ? trainTime * 60 : 7200;
		newPlayer.isVIP = isVIP;//是否是VIP
		player.push_back(newPlayer);//将newPlayer加入到球员队列中
	}
	scanf("%d%d", &k, &m);//球桌数及VIP球桌数
	for (int i = 1; i <= k; i++)
	{
		table[i].endTime = stTime;//当前训练结束时间为8点
		table[i].numServe = table[i].isVIP = 0;//初始化numServe与isVIP
	}
	for (int i = 0; i < m; i++)
	{
		scanf("%d", &VIPtable);//VIP球桌编号
		table[VIPtable].isVIP = 1;//记为VIP球桌
	}
	sort(player.begin(), player.end(), cmpArriveTime);//按到达时间排序
	int i = 0, VIPi = -1;//i用来扫描所有球员,VIPi总是指向当前最前的VIP球员
	VIPi = nextVIPPlayer(VIPi);//找到第一个VIP球员
	while (i < player.size())//当前队列最前面的球员为i
	{
		int idx = -1, minEndTime = INF;//寻找最早能空闲的球桌
		for (int j = 1; j <= k; j++)
		{
			if (table[j].endTime < minEndTime)
			{
				minEndTime = table[j].endTime;
				idx = j;
			}
		}
		//idx为最早空闲的球桌编号
		if (table[idx].endTime >= edTime)break;//已经关门,直接break
		if (player[i].isVIP == 1 && i < VIPi)
		{
			i++;//如果i号是VIP球员,但是VIPi>i,说明i号球员已经在训练
			continue;
		}
		//以下按球桌是否是VIP、球员是否是VIP,进行4种情况讨论
		if (table[idx].isVIP == 1)
		{
			if (player[i].isVIP == 1)//①球桌是VIP,球员是VIP
			{
				allotTable(i, idx);//将球桌idx分配给球员i
				if (VIPi == i)VIPi = nextVIPPlayer(VIPi);//找到下一个VIP球员
				i++;//i号球员开始训练,因此继续队列的下一个人
			}
			else
			{//如果当前队首的VIP球员比该VIP球桌早,就把球桌idx分配给他
				if (VIPi < player.size() && player[VIPi].arriveTime <= table[idx].endTime)
				{
					allotTable(VIPi, idx);//将球桌idx分配给球员i
					VIPi = nextVIPPlayer(VIPi);//找到下一个VIP球员
				}
				else
				{//队首VIP球员比该VIP球桌迟,仍然把球桌idx分配给球员i
					allotTable(i, idx);//将球桌idx分配给球员i
					i++;//i号球员开始训练,因此继续队列的下一个人
				}
			}
		}
		else
		{
			if (player[i].isVIP == 0)//③球桌不是VIP,球员不是VIP
			{
				allotTable(i, idx);//将球桌idx分配给球员i
				i++;//i号球员开始训练,因此继续队列的下一个人
			}
			else
			{//④球桌不是VIP,球员是VIP
				//找到最早空闲的VIP球桌
				int VIPidx = -1, minVIPEndTime = INF;
				for (int j = 1; j <= k; j++)
				{
					if (table[j].isVIP == 1 && table[j].endTime < minVIPEndTime)
					{
						minVIPEndTime = table[j].endTime;
						VIPidx = j;
					}
				}
				//最早空闲的VIP球桌编号是VIPidx
				if (VIPidx != -1 && player[i].arriveTime >= table[VIPidx].endTime)
				{//如果VIP球桌存在,且空闲时间比球员来的时间早
				//就把它分配给球员i
					allotTable(i, VIPidx);
					if (VIPi == i)VIPi = nextVIPPlayer(VIPi);//找到下一个VIP球员
					i++;//i号球员开始训练,因此继续队列的下一个人
				}
				else
				{
					//如果球员来时VIP球桌还未空闲,就把球桌idx分配给他
					allotTable(i, idx);
					if (VIPi == i)VIPi = nextVIPPlayer(VIPi);//找到下一个VIP球员
					i++;//i号球员开始训练,因此继续队列的下一个人
				}
			}
		}
	}
	sort(player.begin(), player.end(), cmpStartTime);//按开始时间排序
	for (int i = 0; i < player.size() && player[i].startTime < edTime; i++)//输出
	{
		int t1 = player[i].arriveTime;
		int t2 = player[i].startTime;
		printf("%02d:%02d:%02d ", t1 / 3600, t1 % 3600 / 60, t1 % 60);
		printf("%02d:%02d:%02d ", t2 / 3600, t2 % 3600 / 60, t2 % 60);
		printf("%.0f\n", round((t2 - t1) / 60.0));
	}
	for (int i = 1; i <= k; i++)
	{
		printf("%d", table[i].numServe);
		if (i < k)printf(" ");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值