栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【PAT A-1026】Table Tennis

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PAT A-1026】Table Tennis

【PAT A-1026】Table Tennis C++代码
#include 
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 players;
    priority_queue 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 ans(ki);  //存储每个球台接待的次数
    vector vip(ki);  //球台是否为vip球台
    while (mi--) {
        cin >> ai;
        vip[ai - 1] = true;  //球台从0~k-1编号
    }
    set freeTables, freeVipTables;  //空闲的所有球台、空闲的vip球台
    list 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 << "0n";
            }
        } 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号