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 privilege 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
心得
这个题,有点麻烦,有大量的测试用例,很奇怪,很多都是看的别人的测试用例,来调的bug,自己写的只通过的一个,后来看的测试用例发现自己真是漏洞百出。
测试链接
这题说实话做的时候觉得是稍稍麻烦一点,但真的没有想到有这么多,该题有很多东西。
- It is assumed that every pair of players can play for at most 2 hours.他假设每个最多两个小时,我以为是他给的例子就不会超过两小时,属实没想到是两小时强制下机。。。之前感觉这种假设都是默认测试里面不会出现的啊,不知道在这是怎么回事!
- if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.如果没有VIP桌子,也可以用正常桌子,这句话代表这个B优先选择VIP桌子。佛了。
- 还有就是一些四舍五入这种常见的,这种题干说了,不是问题。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Customer
{
int aTime;//到达时间
int pTime;//玩的时间
int dTime=0;//开始玩的时间
int VIP;
};
vector<Customer> customers;
int N;
int K, M;
int VipTable[100];//是不是vip桌子
int tableTime[100];//结束时间
int tableNum[100];
bool doSort(Customer c1, Customer c2) {
return c1.aTime < c2.aTime;
}
bool sortE(Customer c1, Customer c2) {
return c1.dTime < c2.dTime;
}
string ITS(int time) {
int HH, MM, SS;
string a, b, c;
SS = time % 60; time = time / 60;
if (SS < 10) a = "0" + to_string(SS);
else a = to_string(SS);
MM = time % 60; time = time / 60;
if (MM < 10) b = "0" + to_string(MM);
else b = to_string(MM);
HH = time;
if (HH < 10) c = "0" + to_string(HH);
else c = to_string(HH);
return c + ":" + b + ":" + a;
}
int STI(string time) {
int h, m, s;
h = (time[0] - '0') * 10 + (time[1] - '0');
m = (time[3] - '0') * 10 + (time[4] - '0');
s = (time[6] - '0') * 10 + (time[7] - '0');
return h * 3600 + m * 60 + s;
}
int findMin() {
int mintime = 99999;
int index = 0;
for (int i = 0; i < K; i++)
{
if (tableTime[i] < mintime) {
index = i;
mintime = tableTime[i];
}
}
return index;
}
int findMinVIP() {
int mintime = 99999;
int index = 0;
for (int i = 0; i < K; i++)
{
if (VipTable[i] == 0)continue;
if (tableTime[i] < mintime) {
index = i;
mintime = tableTime[i];
}
}
return index;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++)
{
Customer customer;
string atime;
cin >> atime >> customer.pTime >> customer.VIP;
customer.aTime = STI(atime);
if (customer.aTime >= 21 * 60 * 60)continue;
customer.pTime *= 60;
if (customer.pTime > 120 * 60)customer.pTime = 120 * 60;
customers.push_back(customer);
}
cin >> K >> M;
for (int i = 0; i < K; i++)
{
tableTime[i] = 8 * 60 * 60;
}
for (int i = 0; i < M; i++)
{
int VN;
cin >> VN;
VipTable[VN - 1] = true;
}
sort(customers.begin(), customers.begin() + customers.size(), doSort);
for (int i = 0; i < customers.size(); i++)
{
if (customers[i].dTime != 0)continue;
int index = findMin();
if (tableTime[index] >= 21 * 3600)//超时了
{
break;
}
else {
if (tableTime[index] <= customers[i].aTime) {//无人排队
if (customers[i].VIP == 1) {
int vipIndex = findMinVIP();
if (tableTime[vipIndex] <= customers[i].aTime) {
tableNum[vipIndex]++;
customers[i].dTime = customers[i].aTime;
tableTime[vipIndex] = customers[i].aTime + customers[i].pTime;
continue;
}
}
tableNum[index]++;
customers[i].dTime = customers[i].aTime;
tableTime[index] = customers[i].aTime+customers[i].pTime;
}
else {
tableNum[index]++;
if (VipTable[index] == 1) {//代表是VIP
bool wfk = true;
for (int j = i; j < N; j++)
{
if (customers[j].VIP == 1&& customers[j].dTime==0) {
customers[j].dTime = tableTime[index];
tableTime[index] += customers[j].pTime;
i = i - 1;
wfk = false;
break;
}
}
if (wfk) {
customers[i].dTime = tableTime[index];
tableTime[index] += customers[i].pTime;
}
}
else {
customers[i].dTime = tableTime[index];
tableTime[index] += customers[i].pTime;
}
}
}
}
//搜寻结束时间最早的桌子 | 当最早的超过5.00,忽略之后的客人
//如果该桌子是VIP桌子,去队列里面找一个VIP出来
//把客人安排上去 //对当前桌子服务人数记录一下
sort(customers.begin(), customers.begin() + customers.size(), sortE);
for (int i = 0; i < customers.size(); i++)
{
if (customers[i].dTime != 0) {
cout << ITS(customers[i].aTime) << " " << ITS(customers[i].dTime) << " " << ((customers[i].dTime) - (customers[i].aTime) + 30) / 60 << "\n";
}
}
for (int i = 0; i < K; i++)
{
if (i != 0)cout << " ";
cout << tableNum[i];
}
return 0;
}