PAT练习(4)-1044 Table Tennis (30)

题目地址:https://www.nowcoder.com/pat/5/problem/4013

题目描述

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.

输入描述:

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.


输出描述:

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.

输入例子:

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

输出例子:

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队列中第一个人早到达),到达时间为Tcur,检测最近的将来空出来的桌子的时间Tmin:

如果Tmin > Tcur,该运动员一定去Tmin对应的桌子进行运动;

如果Tmin <= Tcur,说明有空闲的桌子,如果有vip桌子空着,优先选择该vip桌子,否则选择序号最小的桌子;

③如果当前是一个非vip运动员到达,到达时间为Tcur,检测最近将空出来的桌子的时间为Tmin:

如果Tmin <= Tcur,说明不需要等待(同时,一定没有vip运动员在等待),选择序号最小的桌子;

如果Tmin > Tcur,说明需要等待,这时可能也有vip运动员在等待哦:

能找到到达时间小于等于Tmin的vip运动员,且Tmin对应的是vip桌子,将桌子分配给vip运动员;

不能找到,将这张桌子分配给该非vip运动员。


如果以桌子空闲时间为对象,逻辑应该完全不同了。


首先需要仔细阅读清除题目,不然会混淆一些内容,我遇到的主要的坑是:

①当一个vip人来的时候,如果这时有vip桌子空闲,一定要给他分配vip桌子。


代码:

#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;

#define N 100000 // players
#define K 100 // tables
#define M 100 // vip tables

int n, k, m;

struct  play
{
	int come;
	int period;
};

bool cmpPlay(play p1, play p2)
{
	return p1.come < p2.come;
}

struct playfinish
{
	int come;
	int in;
};

vector<play> vips;
vector<play> normals;
vector<playfinish> finished;

bool isTableVip[K];
int tableTime[K]; // 最后空闲的时间
int servers[K]; //

void findMinTime(int & mintime,int & index) // 找到最近可以提供服务的时间
{
	mintime = INT_MAX;
	for (int i = 0; i < k; ++i)
	{
		if (tableTime[i] < mintime)
		{
			mintime = tableTime[i];
			index = i;
		}
	}
}

int main()
{
	cin >> n;
	int thour, tminite, tsecod, tvip;
	for (int i = 0; i < n; ++i)
	{
		play p;
		scanf("%d:%d:%d", &thour, &tminite, &tsecod);
		p.come = thour * 3600 + tminite * 60 + tsecod;
		cin >> p.period;
		p.period = p.period * 60;
		if (p.period > 7200)
			p.period = 7200;
		cin >> tvip;
		if (tvip)
			vips.push_back(p);
		else
			normals.push_back(p);
	}
	cin >> k >> m;
	for (int i = 0; i < k; ++i)
	{
		tableTime[i] = 0;
		servers[i] = 0;
	}
	int tmp;
	for (int i = 0; i < m; ++i)
	{
		cin >> tmp;
		isTableVip[tmp - 1] = true;
	}


	sort(vips.begin(), vips.end(), cmpPlay);
	sort(normals.begin(), normals.end(), cmpPlay);

	while (vips.size() || normals.size())
	{
		int mintime, index;

		int curtime = INT_MAX;
		bool iscurvip;
		play cur;
		if (vips.size())
		{
			curtime = vips[0].come;
			cur = vips[0];
			iscurvip = true;
		}
		if (normals.size())
			if (normals[0].come < curtime)
			{
				curtime = normals[0].come;
				cur = normals[0];
				iscurvip = false;
			}

		if (curtime>=21*3600)
			break;

		findMinTime(mintime, index);
		if (mintime>=21*3600)
			break;

		if (iscurvip) // vip任务
		{
			if (mintime<=curtime) // 有空
			{
				for (int i = 0; i < k; ++i) // 找到序号最小的桌子
				{
					if (tableTime[i] <= curtime)
					{
						index = i;
						break;
					}
				}
				for (int i = 0; i < k; ++i) // 如果有vip桌子空出来,优先选择vip桌子
				{
					if (tableTime[i] <= curtime && isTableVip[i])
					{
						index = i;
						break;
					}
				}
				tableTime[index] = cur.come + cur.period;
				servers[index]++;
				playfinish finish;
				finish.come = cur.come;
				finish.in = cur.come;
				finished.push_back(finish);
				vips.erase(vips.begin());
// 				for (int i = 0; i < k; ++i)
// 				{
// 					if (tableTime[i]<=curtime)
// 					{
// 						tableTime[i] = cur.come + cur.period;
// 						servers[i]++;
// 						playfinish finish;
// 						finish.come = cur.come;
// 						finish.in = cur.come;
// 						finished.push_back(finish);
// 						vips.erase(vips.begin());
// 						break;
// 					}
// 				}
			}
			else //没有空,就选择等待时间最短的
			{
				playfinish finish;
				finish.come = cur.come;
				finish.in = mintime;
				finished.push_back(finish);
				tableTime[index] = mintime + cur.period;
				servers[index]++;
				vips.erase(vips.begin());
			}
		}
		else // normal任务
		{
			if (mintime <= curtime) // 有空,选序号最小的
			{
				for (int i = 0; i < k; ++i)
				{
					if (tableTime[i] <= curtime)
					{
						tableTime[i] = cur.come + cur.period;
						servers[i]++;
						playfinish finish;
						finish.come = cur.come;
						finish.in = cur.come;
						finished.push_back(finish);
						normals.erase(normals.begin());
						break;
					}
				}
			}
			else // 没空
			{
// 				playfinish finish;
// 				finish.come = cur.come;
// 				finish.in = mintime;
// 				finished.push_back(finish);
// 				tableTime[index] = mintime + cur.period;
// 				servers[index]++;
				
				// 有vip在等待
				// 有没有同一时间空出vip桌子
				if (vips.size())
				{
					for (int i = 0; i < k; ++i)
					{
						if (tableTime[i] <= vips[0].come && isTableVip[i])
						{
							index = i;
							break;
						}
					}
				}

				if (vips.size() &&
					vips[0].come <= mintime &&
					isTableVip[index])
				{
					tableTime[index] = mintime + vips[0].period;
					servers[index]++;
					playfinish f;
					f.come = vips[0].come;
					f.in = mintime;
					finished.push_back(f);
					vips.erase(vips.begin());
				}
				else
				{
					findMinTime(mintime, index);
					tableTime[index] = mintime + cur.period;
					servers[index]++;
					playfinish f;
					f.come = cur.come;
					f.in = mintime;
					finished.push_back(f);
					normals.erase(normals.begin());
				}
			}
		}

	}

	for (int i = 0; i < finished.size(); ++i)
	{
		playfinish & f = finished[i];
		printf("%02d:%02d:%02d %02d:%02d:%02d ", f.come / 3600, f.come / 60 % 60, f.come % 60,
			f.in / 3600, f.in / 60 % 60, f.in % 60);
		cout << ((f.in - f.come) / 60 + ((f.in - f.come) % 60 ? 1 : 0)) << endl;
	}
	cout << servers[0];
	for (int i = 1; i < k; ++i)
	{
		cout << " " << servers[i];
	}

	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值