PAT (Advanced Level) Practice - 1026 Table Tennis

18 篇文章 0 订阅
2 篇文章 0 订阅

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

题意

类似银行排序问题,不过这里增加了VIP。特殊规则在于当空出来的是VIP桌的时候,要看一下当前队列中有没有VIP,如果有要优先VIP,我在实现的时候将VIP、非VIP直接分开存放。

坑提示:

  • 如果按时间排,已经轮到VIP了,还是要看一下有没有VIP桌空着,因为在同时存在非VIP桌、VIP桌空着时,VIP要优先使用VIP桌,这个坑测试点5、7会卡。
  • 如果到了关门时间,还没开始玩,就直接忽略。有两个情况:1是到关门时间之后才到的(arriveTime > closeTime);2是排队要排到关门时间之后才能轮到(arriveTime+waitTime > closeTime)。
  • 题目最后有提示,“The waiting time must be rounded up to an integer minute(s)”,就是说,对于等待时间要四舍五入

还有,跟银行排序相同,这里我们把时间全部化作秒,比如08:00:00等于2880,这样易于做比较以及相加减。

代码

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

#define MAXK 105

int OpenTime = 28800; //08:00:00
int CloseTime = 75600; //21:00:00
int Table[MAXK];
int IsVIP[MAXK], TableCnt[MAXK] = { 0 };
int iv = -1;

struct Player
{
	int ArriveTime, WaitTime, ServeTime, PlayTime;
	bool operator <(const Player& p) const
	{
		return ArriveTime < p.ArriveTime;
	}
};

vector<Player> players, VIP;

double round(double r)
{
    return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}

int findNextTable(int N, int time)
{
    iv = -1;
	int index = -1;
	for (int i = 1; i <= N; ++i)
	{
	    Table[i] = max(Table[i], time);//更新当前桌台结束时间
		if (Table[i] < CloseTime)
		{
		  if(IsVIP[i] && (iv==-1 || Table[i] < Table[iv]))//记录最早结束的VIP桌桌号
		  {
		    iv = i;
		  }
	      if (index == -1 || Table[i] < Table[index])//记录最早结束的非VIP桌桌号
		    index = i;
		}
	}
	  
	return index;
}

int main()
{
	int N, hh, mm, ss, PlayTime, isVIP, K, M, first;
	Player player;
	fill(Table, Table + MAXK, OpenTime);

	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &PlayTime, &isVIP);
		player.ArriveTime = hh * 3600 + mm * 60 + ss;//化秒
		if (player.ArriveTime > CloseTime)
			continue;
		PlayTime *= 60;//化秒
		player.PlayTime = 7200 < PlayTime ? 7200 : PlayTime;//超过2小时按2小时算
		if (isVIP)
			VIP.push_back(player);
		else
			players.push_back(player);
	}

	cin >> K >> M;
	for (int i = 0, tmp; i < M; ++i)
	{
		cin >> tmp;
		IsVIP[tmp] = 1;
	}

	sort(players.begin(), players.end());
	sort(VIP.begin(), VIP.end());

	int index, i = 0, j = 0, iSize = players.size(), jSize = VIP.size(), time = 0;
	while (i < iSize || j < jSize)
	{
		isVIP = 0;
		//先仅按时间来排,看排到的是VIP还是非VIP
		if (i<iSize &&(j >= jSize || players[i].ArriveTime < VIP[j].ArriveTime))
		{
		    time = players[i].ArriveTime;//得到当前队列中最早到达时间
			isVIP = 0;
		}
		else
		{
		    time = VIP[j].ArriveTime;
			isVIP = 1;
		}
		
		index = findNextTable(K, time);
		if (index == -1)
			break;
    
        //当按时间,排到非VIP时,要查看2点
        //1. VIP队列还有没有人,如果有,进2
        //2. 查看当前结束的桌台是不是VIP桌,是的话要给队列中已经到了的VIP(这里包含了要判断桌台结束时间与VIP到达时间)
		if (isVIP==0 && j<jSize && IsVIP[index] && VIP[j].ArriveTime <= Table[index])
		{
			isVIP = 1;
		}
		else if(isVIP == 1 && !IsVIP[index])//如果按时间也是排到了VIP,
		{
		  if(Table[iv] <= VIP[j].ArriveTime)//则要看一下有没有VIP桌台空着(包含了判断时间)
		    index = iv;                     //如果有,则VIP在同时有VIP桌、非VIP桌空着时,优先选择VIP桌
		}
		
		++TableCnt[index];
		
		if (isVIP == 1)
		{
			VIP[j].ServeTime = Table[index];
			VIP[j].WaitTime = VIP[j].ServeTime - VIP[j].ArriveTime;
			Table[index] += VIP[j].PlayTime;
			++j;
		}
		else 
		{
			players[i].ServeTime = Table[index];
			players[i].WaitTime = players[i].ServeTime - players[i].ArriveTime;
			Table[index] += players[i].PlayTime;
			++i;
		}
	}

	Player p;
	for (int k1 = 0, k2 = 0; k1 < i || k2 < j; )
	{
		if (k1<i && ( k2>=j || players[k1].ServeTime < VIP[k2].ServeTime))
		{
			p = players[k1];
			++k1;
		}
		else
		{
			p = VIP[k2];
			++k2;
		}

		int time = p.ArriveTime;
		int h1 = time / 3600, m1 = (time % 3600) / 60, s1 = (time % 3600) % 60;
		time = p.ServeTime;
		int h2 = time / 3600, m2 = (time % 3600) / 60, s2 = (time % 3600) % 60;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", h1, m1, s1, h2, m2, s2, (int)round(p.WaitTime / 60.0));
	}

	cout << TableCnt[1];
	for (int i = 2; i <= K; ++i)
		cout << " " << TableCnt[i];
	cout << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值