【PAT A-1014】Waiting in Line

【PAT A-1014】Waiting in Line

C++代码

#include <bits/stdc++.h>
using namespace std;
using gg = long long;
struct Customer {
    gg endTime, num, w;  //结束服务的时间、顾客编号、服务窗口编号
    Customer(gg e, gg n, gg ww) : endTime(e), num(n), w(ww) {}
};
bool operator<(const Customer& c1, const Customer& c2) {
    return tie(c1.endTime, c1.w) > tie(c2.endTime, c2.w);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    gg ni, mi, ki, qi, ai, endTime = 17 * 60;
    cin >> ni >> mi >> ki >> qi;
    vector<gg> ans(ki, INT_MAX);  // INT_MAX默认为该顾客没有接受服务
    //目前排在黄线内的所有顾客信息
    priority_queue<Customer> customers;
    // peopleNum存储每个窗口的排队人数、time存储每个窗口排在最后的人处理完成的时间
    vector<gg> peopleNum(ni), time(ni, 8 * 60);
    for (gg i = 0; i < ki; ++i) {
        cin >> ai;  //读取顾客需要的服务时间
        //黄线内人数达到上限,从队列中弹出一个顾客
        if (customers.size() == ni * mi) {
            auto c = customers.top();
            customers.pop();
            peopleNum[c.w]--;
        }
        gg j = min_element(peopleNum.begin(), peopleNum.end()) - peopleNum.begin();
        peopleNum[j]++;
        if (time[j] >= endTime) {  //开始服务时间超过17:00,该顾客不能被服务
            continue;
        }
        time[j] += ai;
        customers.push(Customer(time[j], i, j));
        ans[i] = time[j];
    }
    for (gg i = 0; i < qi; ++i) {
        cin >> ai;
        if (ans[ai - 1] == INT_MAX) {
            cout << "Sorry\n";
        } else {
            cout << setfill('0') << setw(2) << ans[ai - 1] / 60 << ":" << setw(2)
                 << ans[ai - 1] % 60 << "\n";
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我真的不是cjc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值