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
我的思路:
将玩家按到达顺序一个一个往里面放,先定义一个当前时间(nowTime),如果玩家是在当前时间之后才到达,那么把时间调到玩家到达的时间,相应的要减少每桌的被占用时间;
然后找最快结束占用的桌子,将时间调到该桌子结束时刻,同时减少每桌的占用时间;那么现在就是必有空桌(最快空桌可能不止一桌);然后找是否有空vip桌和vip玩家,都有则交换vip和i的位置,因为要先处理vip,否则就找最小编号空桌;最后将i放入vip桌或最小编号空桌内。
个人感觉思路没问题,但是最后一个就是过不了,最后一个测试N=10000;希望各位帮忙看看,感谢。
已解决:
问题在这
if(i!=vip)
swap(persons[i],persons[vip]);
比如1 2 4 7 9(vip),swap之后9(vip) 2 4 7 1,2 4 7 1就有问题,应该是
if(…)
for(j=vip;j>i;j–)
swap(persons[j],persons[j-1]);
代码:
#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#pragma warning(disable:4996)
struct Person
{
int arriveTime, lastTime, isVIP;
int startTime;
}persons[10010];
int N, K, M;//人数,table数,vip table数
int tables[110][2];//[0]球桌将被占用时长,[1]是否是vip桌
int server[110];//球桌的服务数量
int nowTime = 8 * 3600;//从8点开门开始
int endTime = 21 * 3600;
bool cmp(Person a, Person b)
{
return a.arriveTime < b.arriveTime;
}
bool cmp_2(Person a, Person b)
{
return a.startTime < b.startTime;
}
int findEmptyTable(int isVip)
{
int i;
int emptyTable = -1;
for (i = 0; i < K; i++)
{
if (tables[i][0] == 0)
{
if (isVip == 1 && tables[i][1] != 1)
continue;
emptyTable = i;
break;
}
}
return emptyTable;
}
int findVip(int start, int nowTime)
{
int j, vip = -1;
for (j = start; j < N&&persons[j].arriveTime <= nowTime; j++)
{
if (persons[j].isVIP)
{
vip = j;
break;
}
}
return vip;
}
int main()
{
int i, j, h, m, s;
//input
scanf("%d", &N);
for (i = 0; i < N; i++)
{
scanf("%d:%d:%d %d %d", &h, &m, &s, &persons[i].lastTime, &persons[i].isVIP);
persons[i].arriveTime = h * 3600 + m * 60 + s;
persons[i].startTime = endTime + 1;
persons[i].lastTime = persons[i].lastTime > 120 ? 120 : persons[i].lastTime;//时间不超过两小时
persons[i].lastTime *= 60;
}
cin >> K >> M;//K个窗口,M个VIP
for (i = 0; i < K; i++)
tables[i][0] = tables[i][1] = server[i] = 0;
for (i = 0; i < M; i++)
{
cin >> j;
tables[j - 1][1] = 1;
}
sort(persons, persons + N, cmp);
int emptyTable, vipTable, fastestTable, vip;
for (i = 0; i < N; i++)
{
//如果当前时刻该顾客还未到,时间调到顾客到达时间
if (persons[i].arriveTime > nowTime)
{
//所有table经过arriveTime-nowTime时长
for (j = 0; j < K; j++)
{
tables[j][0] -= (persons[i].arriveTime - nowTime);
if (tables[j][0] < 0)
tables[j][0] = 0;
}
nowTime = persons[i].arriveTime;
}
//寻找最快结束的桌子
fastestTable = 0;
for (j = 1; j < K; j++)
{
if (tables[j][0] < tables[fastestTable][0])
fastestTable = j;
}
//时间调到最快结束桌子的结束时间
nowTime += tables[fastestTable][0];
if (nowTime >= endTime)//当有空桌时,已关门,后面的人舍弃
{
N = i;
break;
}
int t = tables[fastestTable][0];
for (j = 0; j < K; j++)
tables[j][0] -= t;
//现在必有空桌
vipTable = findEmptyTable(1);
vip = findVip(i, nowTime);
//1,有vip空桌时且当前时间有vip,vip优先
if (vipTable != -1 && vip != -1)
{
if (i != vip)
swap(persons[i], persons[vip]);
emptyTable = vipTable;
}
else//2,当前时刻,vip空桌和vip不同时存在
{
emptyTable = findEmptyTable(0);
if (emptyTable == -1)
while (1);
}
persons[i].startTime = nowTime;
tables[emptyTable][0] = persons[i].lastTime;
server[emptyTable]++;
}
//sort(persons, persons + N, cmp_2);
//output
int wait;
for (i = 0; i < N; i++)
{
if (persons[i].startTime >= endTime)
while (1);
printf("%02d:%02d:%02d %02d:%02d:%02d", persons[i].arriveTime / 3600, (persons[i].arriveTime / 60) % 60, persons[i].arriveTime % 60, persons[i].startTime / 3600, (persons[i].startTime / 60) % 60, persons[i].startTime % 60);
wait = persons[i].startTime - persons[i].arriveTime;
wait = wait / 60 + (wait % 60) / 30;
printf(" %d\n", wait);
}
for (i = 0; i < K - 1; i++)
printf("%d ", server[i]);
printf("%d\n", server[i]);
return 0;
}
/*
注意:
1,窗口编号1 - K
2,用for(;a[i].x<b;i++)时,要加i<N;(智障一样写成i<K;浪费几个小时时间(F10单步调试牛逼))不然可能越界
3,输入数据有大于玩两小时的,要将其降为两小时
4,过了21点还没开始玩的要舍弃
*/