【PAT A-1026】Table Tennis
C++代码
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
struct Player {
gg arrive, server, vip;
Player(gg a, gg s, gg v) : arrive(a), server(s), vip(v) {}
};
struct Action {
gg tableNum, playerNum, time;
Action(gg t, gg p, gg ti) : tableNum(t), playerNum(p), time(ti) {}
};
bool operator<(const Action& a1, const Action& a2) { return a1.time > a2.time; }
void output(gg time) {
cout << setfill('0') << setw(2) << time / 3600 << ":" << setw(2)
<< time % 3600 / 60 << ":" << setw(2) << time % 60 << " ";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
gg ni, ki, mi, ai;
cin >> ni;
gg endTime = 21 * 3600;
vector<Player> players;
priority_queue<Action> actions;
for (gg i = 0; i < ni; ++i) {
gg h, m, s, p, vip;
char c;
cin >> h >> c >> m >> c >> s >> p >> vip;
players.push_back(
Player(h * 3600 + m * 60 + s, min(p, 120ll) * 60, vip));
actions.push(Action(-1, i, players[i].arrive));
}
cin >> ki >> mi;
vector<gg> ans(ki);
vector<bool> vip(ki);
while (mi--) {
cin >> ai;
vip[ai - 1] = true;
}
set<gg> freeTables, freeVipTables;
list<gg> waitPlayers, waitVipPlayers;
for (gg i = 0; i < ki; ++i) {
freeTables.insert(i);
if (vip[i]) {
freeVipTables.insert(i);
}
}
while (not actions.empty()) {
auto a = actions.top();
actions.pop();
if (a.time >= endTime) {
break;
}
if (a.tableNum == -1) {
if (freeTables.empty()) {
waitPlayers.push_back(a.playerNum);
if (players[a.playerNum].vip) {
waitVipPlayers.push_back(a.playerNum);
}
} else {
gg num = -1;
if (players[a.playerNum].vip and not freeVipTables.empty()) {
num = *freeVipTables.begin();
freeVipTables.erase(num);
freeTables.erase(num);
} else {
num = *freeTables.begin();
freeTables.erase(num);
if (freeVipTables.count(num)) {
freeVipTables.erase(num);
}
}
actions.push(Action(num, a.playerNum,
a.time + players[a.playerNum].server));
++ans[num];
output(a.time);
output(a.time);
cout << "0\n";
}
} else {
if (waitPlayers.empty()) {
freeTables.insert(a.tableNum);
if (vip[a.tableNum]) {
freeVipTables.insert(a.tableNum);
}
} else {
gg num = -1;
if (vip[a.tableNum] and not waitVipPlayers.empty()) {
num = waitVipPlayers.front();
waitVipPlayers.pop_front();
waitPlayers.erase(
find(waitPlayers.begin(), waitPlayers.end(), num));
} else {
num = waitPlayers.front();
waitPlayers.pop_front();
auto i =
find(waitVipPlayers.begin(), waitVipPlayers.end(), num);
if (i != waitVipPlayers.end()) {
waitVipPlayers.erase(i);
}
}
actions.push(
Action(a.tableNum, num, a.time + players[num].server));
++ans[a.tableNum];
output(players[num].arrive);
output(a.time);
cout << round((a.time - players[num].arrive) / 60.0) << "\n";
}
}
}
for (gg i = 0; i < ki; ++i) {
cout << (i == 0 ? "" : " ") << ans[i];
}
return 0;
}