【PAT A-1026】Table Tennis

【PAT A-1026】Table Tennis

C++代码

#include <bits/stdc++.h>
using namespace std;
using gg = long long;
struct Player {  //运动员
    gg arrive, server, vip;  //到达时间、打球所需时间、是否为vip
    Player(gg a, gg s, gg v) : arrive(a), server(s), vip(v) {}
};
// tableNum==-1,表示playerNum号运动员于time时间到达,需分配空闲球台
// tableNum!=-1,表示playerNum号运动员占据了tableNum号球台,在time时间会离开
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;
        //注意,打球时间超过2小时,按2小时计!
        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);  //球台是否为vip球台
    while (mi--) {
        cin >> ai;
        vip[ai - 1] = true;  //球台从0~k-1编号
    }
    set<gg> freeTables, freeVipTables;  //空闲的所有球台、空闲的vip球台
    list<gg> waitPlayers, waitVipPlayers;  //等待的所有球员、等待的vip球员
    //初始时所有球台均闲置
    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;  //存储分配的球台编号
                //是vip球员且有空闲的vip球台,分配编号最小的vip球台
                if (players[a.playerNum].vip and not freeVipTables.empty()) {
                    num = *freeVipTables.begin();
                    freeVipTables.erase(num);
                    freeTables.erase(num);
                } else {  //不是vip球员或是vip球员但没有空闲的vip球台,分配普通球台
                    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;  //存储将该球台分配给的球员编号
                //是vip球台且有vip球员等待,分配给队列首位的vip球员
                if (vip[a.tableNum] and not waitVipPlayers.empty()) {
                    num = waitVipPlayers.front();
                    waitVipPlayers.pop_front();
                    waitPlayers.erase(
                        find(waitPlayers.begin(), waitPlayers.end(), num));
                } else {  //不是vip球台或是vip球台但没有vip球员等待,分配给队列首位的普通球员
                    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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我真的不是cjc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值