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

思路:

典型的事件驱动模拟

遇到的问题:

1. 有等待的vip选手,且有空闲的vip 球桌,则优先使用序号小的vip球桌,不然vip和普通选手一样;

2. 事件触发的优先级:触发事件->VIP->到达时间(最后一个测试点会涉及);

3. 持续时间需要截断(2hours);

#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <deque>
#include <cstdio>
using namespace std;

#define CLOSETIME 21*3600
#define OPENTIME 8*3600

struct table {
	table():vip_tag(0),count(0), busy(0){};
	int vip_tag;
	int count;
	int busy;
};

struct player {
	player():arrive_h(0),arrive_m(0),arrive_s(0), during_time(0), vip(0), table(-1){};
	int arrive_h;
	int arrive_m;
	int arrive_s;

	int during_time;
	int vip;
	int table;
};

struct Event {
	Event(player p, int h, int m, int s, int t):p(p),occur_h(h), occur_m(m), occur_s(s),type(t){};
	player p;
	int occur_h;
	int occur_m;
	int occur_s;
	int type;
};

struct cmp {
	bool operator()(Event &a, Event &b)
	{
		int t1 = a.occur_h*3600 + a.occur_m* 60 + a.occur_s;
		int t2 = b.occur_h*3600 + b.occur_m*60 + b.occur_s;

		if(t1 == t2)
		{//事件触发事件优先
			if(a.p.vip == b.p.vip)
			{//对于最后一个case很重要, 事件触发时间相同的情况下VIP优先,VIP相同的情况下到达时间优先
				int at1 = a.p.arrive_h* 3600+a.p.arrive_m*60+a.p.arrive_s;
				int at2 = b.p.arrive_h* 3600+b.p.arrive_m*60+b.p.arrive_s;
				return at1 > at2;
			}
			return a.p.vip < b.p.vip;
		}
		return t1 > t2;
	}
};

int main()
{
	int N;
	scanf("%d", &N);
	vector<player> players(N);
	int i;

	for(i = 0; i < N; i++)
	{
		player &t = players[i];
		scanf("%d:%d:%d %d %d", &t.arrive_h, &t.arrive_m,&t.arrive_s, &t.during_time,&t.vip);
		if(t.during_time > 120)
			t.during_time = 120;
	}

	int K, M;
	scanf("%d %d", &K, &M);
	vector<table> tables(K);
	for(i = 0; i< M; i++)
	{
		int t;
		scanf("%d", &t);
		tables[t-1].vip_tag = 1;
	}

	priority_queue<Event,vector<Event>,cmp> eq;
	deque<Event> q;

	for(i = 0; i< N; i++)
	{
		int tt = 3600*players[i].arrive_h+60*players[i].arrive_m+players[i].arrive_s;
		if(tt <  CLOSETIME && tt >= OPENTIME)
			eq.push(Event(players[i],players[i].arrive_h,players[i].arrive_m, players[i].arrive_s, 0));
	}

	while(!eq.empty())
	{
		Event e = eq.top();
		eq.pop();

		if(e.type == 0)
		{//arrive event
			int j;
			if(e.p.vip)
			{//VIP prio to select vip table firstly
				int v = -1;
				for(j = 0; j< K; j++)
				{//find a free table
					if(!tables[j].busy )
					{
						if(v == -1)
							v = j;
						if(tables[j].vip_tag)
							break;
					}
				}
				if(j == K)
					if(v != -1)
						j = v;

			}
			else
			{
				for(j = 0; j< K; j++)
				{//find a free table
					if(!tables[j].busy)
						break;
				}
			}
			if(j != K)
			{//if j is a free table
				tables[j].busy = 1;
				tables[j].count++;
				e.p.table = j;
				int during_time = e.p.during_time;
				int s = e.occur_s;
				int m = e.occur_m+during_time;
				int h = e.occur_h + m / 60;
				eq.push(Event(e.p, h,m %60, s, 1));
			//output the result
				int wait_t = ( 3600*e.occur_h+60*e.occur_m+e.occur_s - 3600*e.p.arrive_h-60*e.p.arrive_m-e.p.arrive_s+30)/60;
				printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",e.p.arrive_h,e.p.arrive_m,e.p.arrive_s,e.occur_h,e.occur_m,e.occur_s, wait_t);
			}

			else
			{// if there is no free table
				q.push_back(e);
			}
		}
		else
		{//leave event
			int h = e.occur_h, m = e.occur_m, s = e.occur_s;
			player &p = e.p;
			tables[p.table].busy = 0;

			if(!q.empty())
			{
				int v = 0;
				if(tables[p.table].vip_tag)
				{
					for(v = 0; v < q.size(); v++)
					{
						if(q[v].p.vip)
							break;
					}
					if(v == q.size())
						v = 0;
				}
				int tt = 3600*h + 60*m + s;
				if(tt < CLOSETIME)
				{
					eq.push(Event(q[v].p, h,m,s,0));
					q.erase(q.begin()+v);
				}
				else
				{
					break;
				}
			}
		}
	}
	int flag = 0;
	for(i=0; i< tables.size(); i++)
	{
		if(!flag)
		{
			flag = 1;
			printf("%d",tables[i].count);
		}
		else
			printf(" %d",tables[i].count);
	}
	printf("\n");

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 01:20:04

1026. Table Tennis (30)的相关文章

1026 Table Tennis (30 分)

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 sma

浙大 PAT Advanced level 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

PAT (Advanced Level) 1026. Table Tennis (30)

情况比较多的模拟题. 交了50发的样子才AC......AC之后我的天空星星都亮了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct

PAT甲级1026 Table Tennis【模拟好题】

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560 题意: 有k张乒乓球桌,有的是vip桌.有n对玩家来打乒乓,有的玩家是VIP玩家. 当他们到达时,如果没有空桌子他们就排队等待. 这时候如果有VIP桌子空出来了,那么就优先给队列里的VIP玩家,如果没有VIP玩家就给普通玩家. 如果普通桌子空出来了,就给队列里排在最前的玩家. 如果玩家到达的时候有好多桌子可以选择,那么他会选择桌号最小的

Pat(Advanced Level)Practice--1026(Table Tennis)

Pat1026代码 题目描写叙述: 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 n

CF765C Table Tennis Game 2

题意: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2

地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Misha and Vanya have played several table tennis sets. Each set co

@@443 B. Table Tennis

n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone win

1795. Table tennis

#include<iostream>#include<cmath> using namespace std;int main(){ int n; cin>>n; while(n--){  int t;  cin>>t;  int points=0;  while(t--){   int a,b;   cin>>a>>b;   if((a>10&&a<50)&&(b>10&&am