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 2Sample 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<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<functional>
#include<queue>
#include<set>
using namespace std;
struct TABLE
{
int num;
int end;
int vip;
int sum;
TABLE(int i) :end(8 * 60 * 60), vip(0), sum(0),num(i) {};
/*bool operator <(const TABLE& t)const
{
if (end !=t.end)return end<t.end;
else return num<t.num;
}*/
};
bool operator<(const TABLE& t1,const TABLE& t2)
{
if (t1.end != t2.end)return t1.end<t2.end;
else return t1.num<t2.num;
}
struct CUSTOM
{public:
string arrival;
string serve;
int begin;
int end;
int vip;
int play;
int tag=0;
int wait=WAIT(begin,end);
CUSTOM(){};
CUSTOM(string s,int i,int j) :arrival(s), end(0), vip(j), play(i), tag(0) {
serve = Transstr(end);
begin = Transint(arrival);
};
int Transint(const string& time)
{
int ans = 0;
ans = ((time[0]-'0') * 10 + time[1]-'0') * 3600 +
((time[3]-'0') * 10 + time[4]-'0') * 60 +
(time[6]-'0') * 10 + time[7]-'0';
return ans;
}
string Transstr(int t)
{
int hh = 0, mm = 0, ss = 0;
hh = t / 3600;
mm = t % 3600;
ss = mm % 60;
mm = mm / 60;
string tt;
tt.push_back(hh / 10 + '0');
tt.push_back(hh % 10 + '0');
tt.push_back(':');
tt.push_back(mm / 10 + '0');
tt.push_back(mm % 10 + '0');
tt.push_back(':');
tt.push_back(ss / 10 + '0');
tt.push_back(ss % 10 + '0');
return tt;
}
int WAIT(int b, int e)
{
int res = 0;
res = (int)round((e - b) / 60.00);
return res;
}
/*bool operator <(const CUSTOM& c)const
{
return begin < c.begin;
}*/
};
bool CompA(CUSTOM a, CUSTOM b)
{
return a.begin < b.begin;
}
bool CompB(CUSTOM a, CUSTOM b)
{
return a.end < b.end;
}
int main()
{
int N, i;
string arrival;
int play, vip;
vector<CUSTOM>que;
cin >> N;
for (i = 0; i < N; i++)
{
cin >> arrival >> play >> vip;
if (play > 120)play = 120;
CUSTOM tmp(arrival, play*60, vip);
que.push_back(tmp);
}
sort(que.begin(), que.end(), CompA);
int tnum, vnum, j;
cin >> tnum >> vnum;
vector<int>flag(tnum + 1, 0);
for (i = 1; i <= vnum; i++)
{
cin >> j;
flag[j] = 1;
}
set<TABLE>table;
vector<TABLE>freetable;
vector<CUSTOM>waitingp;
vector<CUSTOM>serve;
for (i = 0; i < tnum; i++)
{
TABLE tab(i + 1);
if (flag[i + 1] == 1)tab.vip = 1;
table.insert(tab);
//freetable.push_back(tab);
}
int nowtime=8*3600;
set<TABLE>::iterator it=table.begin();
CUSTOM cus;
int vt = 0, vc = 0;
while (!que.empty())
{
while(!que.empty() && que[0].begin <= nowtime)
{
waitingp.push_back( que[0]);
if (que[0].vip == 1)vc++;
que.erase(que.begin());
}
if (waitingp.empty())
{
waitingp.push_back(que[0]);
if (que[0].vip == 1)vc++;
nowtime = que[0].begin;
que.erase(que.begin());
}
if (vc != 0)
{
for (i = 0; i < waitingp.size(); i++)
if (waitingp[i].vip == 1)break;
cus = waitingp[i];
waitingp.erase(waitingp.begin()+i);
vc--;
}
else
{
cus = waitingp[0];
waitingp.erase(waitingp.begin());
}
while(!table.empty() && table.begin()->end <= nowtime)
{
it = table.begin();
freetable.push_back(*it);
if (it->vip == 1)vt++;
table.erase(it);//
}
if (!freetable.empty())
{
if (cus.vip == 1 && vt != 0)//vip客户有vip桌子;
{
for (i =0; i < freetable.size(); i++)
if (freetable[i].vip == 1)break;
freetable[i].end = nowtime + cus.play;
freetable[i].sum++;
table.insert(freetable[i]);
freetable.erase(freetable.begin() + i);
vt--;
}
else
{ freetable[0].end= nowtime + cus.play;
freetable[0].sum++;
if (freetable[0].vip == 1)vt--;
table.insert(freetable[0]);
freetable.erase(freetable.begin());
}
cus.end = nowtime;
cus.serve = cus.Transstr(cus.end);
cus.wait = cus.WAIT(cus.begin, cus.end);
if (cus.end >= 21 * 3600)break;
serve.push_back(cus);
}
else
{
if (cus.vip == 1)
{
waitingp.push_back(cus);
vc++;
sort(waitingp.begin(), waitingp.end(),CompA);//
}
else
{
waitingp.insert(waitingp.begin(), cus);
}
nowtime = table.begin()->end;
}
}
for (i = 0; i < serve.size(); i++)
{
cout << serve[i].arrival << " " << serve[i].serve << " " << serve[i].wait << endl;
}
vector<int>tt(tnum + 1);
for (it = table.begin(); it != table.end(); it++)
tt[it->num] = it->sum;
for (i = 1; i <= tnum; i++)
{
if (i == 1)cout << tt[i];
else
cout << " " << tt[i];
}
return 0;
}
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <functional>
using namespace std;
bool table[10005], isvip[10005];
int tableTimes[10005];
struct node{
int arriveTime, useTime;
bool vip;
node(){}
node(int t, int tu, bool v):arriveTime(t), useTime(tu), vip(v){}
bool operator<(const node& b)const{
return arriveTime < b.arriveTime;
}
};
bool operator<(const pair<int,int> &a, const pair<int,int> &b){
return a.first < b.first;
}
set<node> arriveList; //顾客到达列表
priority_queue<pair<int, int>, vector<pair<int,int> >, greater<pair<int,int> > > que; //乒乓球台到期队列
set<int> freeList; //空闲台队列
int main()
{
int n, tableNum, tableVipNum, tableVipNow,a, b, c, t, vip;
int nowTime = 8*3600;
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d:%d:%d%d%d", &a, &b, &c, &t, &vip);
arriveList.insert(node(a*3600 + b*60 + c, t, vip == 1));
}
scanf("%d%d", &tableNum, &tableVipNum);
for (int i = 0; i < tableNum; ++i){
freeList.insert(i);//初始化空闲台
}
tableVipNow = tableVipNum;
for (int i = 0; i < tableVipNum; ++i){
scanf("%d", &vip);
isvip[vip-1] = true;
}
while (!arriveList.empty() && nowTime < 21*3600){
t = arriveList.begin()->arriveTime;
if (!freeList.empty() && t <= nowTime){
if (tableVipNow > 0){
set<node>::iterator it = arriveList.begin();
while (it != arriveList.end() && nowTime >= it->arriveTime){
if (it->vip){
break;
}
++it;
}
if (it != arriveList.end() && nowTime >= it->arriveTime){
--tableVipNow;
set<int>::iterator itable = freeList.begin();
while (!isvip[*itable]){
++itable;
}
t = it->arriveTime;
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t/3600, t%3600/60, t%60, nowTime/3600, nowTime%3600/60, nowTime%60, (nowTime - t + 30)/60); //四舍五入
tableTimes[*itable]++;
que.push(make_pair(nowTime + min(120, it->useTime) * 60, *itable)); //超过两小时截断
freeList.erase(*itable);
arriveList.erase(*it);
continue;
}
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t/3600, t%3600/60, t%60, nowTime/3600, nowTime%3600/60, nowTime%60, (nowTime - t + 30)/60);
tableTimes[*freeList.begin()]++;
que.push(make_pair(nowTime + min(120, arriveList.begin()->useTime) * 60, *freeList.begin())); //截断
if (isvip[*freeList.begin()]){
--tableVipNow;
}
freeList.erase(*freeList.begin());
arriveList.erase(*arriveList.begin());
continue;
}else {
if (que.empty() || (!freeList.empty() && que.top().first > t)){
nowTime = t;
}else{
if (nowTime < que.top().first){
nowTime = que.top().first;
}
while (!que.empty() && nowTime >= que.top().first){
freeList.insert(que.top().second);
if (isvip[que.top().second]) {
++tableVipNow;
}
que.pop();
}
}
}
}
printf("%d", tableTimes[0]);
for (int i = 1; i < tableNum; ++i){
printf(" %d", tableTimes[i]);
}
return 0;
}