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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct person
{
	int arrive, start, time;
	bool vip;
}tempPerson;

struct tablenode
{
	int end = 8 * 3600, num;
	bool vip;
};
const int inf = 999999999;
vector<person> player;
vector<tablenode> table;

bool cmp1(person a, person b)
{
	return a.arrive < b.arrive;
}

bool cmp2(person a, person b)
{
	return a.start < b.start;
}

void allocate(int personid, int tableid)
{
	if (player[personid].arrive <= table[tableid].end)
	{
		player[personid].start = table[tableid].end;
	}
	else
	{
		player[personid].start = player[personid].arrive;
	}
	table[tableid].end = player[personid].start + player[personid].time;
	table[tableid].num++;
}

int findnextvip(int vipid)
{
	vipid++;
	int playerSize = player.size();
	while (vipid < playerSize&&player[vipid].vip == false)
	{
		vipid++;
	}
	return vipid;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int n, k, m;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int h, m, s, temptime, flag;
		scanf("%d:%d:%d %d %d", &h, &m, &s, &temptime, &flag);
		tempPerson.arrive = h * 3600 + m * 60 + s;
		tempPerson.start = 21 * 3600;
		if (tempPerson.arrive >= 21 * 3600)//就因为这里写成了大于而不是大于等于,测试点3不过,可见程序实在太精确了,差一点都不行这也需要对题意和程序的理解
		{
			continue;
		}
		tempPerson.time = temptime < 120 ? temptime * 60 : 2 * 3600;
		tempPerson.vip = ((flag == 0) ? false : true);
		player.push_back(tempPerson);
	}
	scanf("%d %d", &k, &m);
	table.resize(k + 1);
	for (int i = 1; i <= m; i++)
	{
		int viptable;
		scanf("%d", &viptable);
		table[viptable].vip = true;
	}
	sort(player.begin(), player.end(), cmp1);
	int i = 0, vipid = -1;
	vipid = findnextvip(vipid);
	while (i < player.size())
	{
		int tableindex = -1, minendtime =  inf;
		for (int j = 1; j <= k; j++)
		{
			if (table[j].end < minendtime)
			{
				minendtime = table[j].end;
				tableindex = j;
			}
		}
		if (table[tableindex].end >= 21 * 3600)
		{
			break;
		}
		if (player[i].vip == true && i < vipid)
		{//因为vip在等待队列里时,如果有vip桌子可以用,可优先使用,所以只有player[i].vip==true有可能已经分配过了,i<vipid说明已经分配过了,因为vip分配以后,vipid会更新找到下一个vip,所以才有  i是vip的同时i<vipid。
			i++;
			continue;
		}
		if (table[tableindex].vip == true)//table is vip
		{
			if (player[i].vip == true)//table and player are vip
			{
				allocate(i, tableindex);
				if (vipid == i)
				{
					vipid = findnextvip(vipid); 
				}  
				i++;
			}
			else//table is vip and player is not vip
			{
				if (vipid < player.size() && player[vipid].arrive <= table[tableindex].end)//vip已经来了,在等待队列里,所以优先使用这张vip桌
				{// 这里只处理了等待队列里的vip,本来是要给非vip分配桌子的,结果看到队列里的vip,优先给人家分配,所以这里i不变,进入下次循环再次给这个非vip分配桌子直到分配成功,i++;
					allocate(vipid, tableindex);
					vipid = findnextvip(vipid);	
				}
				else//队列里没有vip,vip来得晚,到时候vip桌使用结束了,vip来了 可以直接用
				{
					allocate(i, tableindex);
					i++;
				}
			}
		}
		else//table is not vip
		{
			if (player[i].vip == false)//table and player are not vip
			{
				allocate(i, tableindex);
				i++;
			}
			else//table is not vip and player is vip
			{
				int viptableindex = -1, minVipEndtime =  inf;
				for (int j = 1; j <= k; j++)
				{//找到最早可以用的vip桌
					if (table[j].vip==true&&table[j].end < minVipEndtime)
					{
						minVipEndtime = table[j].end;
						viptableindex = j;
					}
				}
				if (viptableindex!=-1&&player[i].arrive >= minVipEndtime)//vip到的时候,vip桌空闲可用
				{//所以给非vip分配了这张vip桌子
					allocate(i, viptableindex);
					if (vipid == i)
					{
						vipid = findnextvip(vipid);
					}
					i++;
					
				}
				else//vip到的时候没有空闲的vip桌,player当作非vip处理
				{
					allocate(i, tableindex);
					if (vipid == i)
					{
						vipid = findnextvip(vipid);
					}
					i++;
				}
			}
		}
	}
	sort(player.begin(), player.end(), cmp2);
	int playerSize = player.size();
	for (int i = 0; i < playerSize&&player[i].start < 21 * 3600; i++)
	{
		printf("%02d:%02d:%02d ", player[i].arrive / 3600, player[i].arrive % 3600 / 60, player[i].arrive % 60);
		printf("%02d:%02d:%02d ", player[i].start / 3600, player[i].start % 3600 / 60, player[i].start % 60);
		printf("%.0f\n", round((player[i].start - player[i].arrive) / 60.0));
	}
	for (int i = 1; i <= k; i++)
	{
		if (i != 1)
		{
			printf(" ");
		}
		printf("%d", table[i].num);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值