PTA甲级 1014 Waiting in Line (C++)

Suppose a bank has N N N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M M M customers. Hence when all the N N N lines are full, all the customers after (and including) the ( N M + 1 ) (NM+1) (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • C u s t o m e r i Customer_i Customeri will take T i T_i Ti minutes to have his/her transaction processed.
  • The first N N N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 customers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, c u s t o m e r 1 customer_1 customer1 is served at w i n d o w 1 window_1 window1 while c u s t o m e r 2 customer_2 customer2 is served at w i n d o w 2 window_2 window2. C u s t o m e r 3 Customer_3 Customer3 will wait in front of w i n d o w 1 window_1 window1 and c u s t o m e r 4 customer_4 customer4 will wait in front of w i n d o w 2 window_2 window2. C u s t o m e r 5 Customer_5 Customer5 will wait behind the yellow line.

At 08:01, c u s t o m e r 1 customer_1 customer1 is done and c u s t o m e r 5 customer_5 customer5 enters the line in front of w i n d o w 1 window_1 window1 since that line seems shorter now. C u s t o m e r 2 Customer_2 Customer2
will leave at 08:02, c u s t o m e r 4 customer_4 customer4 at 08:06, c u s t o m e r 3 customer_3 customer3 at 08:07, and finally c u s t o m e r 5 customer_5 customer5 at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N N N ( ≤ 20 ≤20 20, number of windows), M M M ( ≤ 10 ≤10 10, the maximum capacity of each line inside the yellow line), K K K ( ≤ 1000 ≤1000 1000, number of customers), and Q Q Q ( ≤ 1000 ≤1000 1000, number of customer queries).

The next line contains K K K positive integers, which are the processing time of the K K K customers.

The last line contains Q Q Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K K K.

Output Specification:

For each of the Q Q Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

08:07
08:06
08:10
17:00
Sorry

Caution:

需要注意的是如果一个顾客在17:00之前开始处理业务,那么会一直处理完,不会在17:00被中止,这是导致我第一次没有AC的主要原因。

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-12 10:08:20
// All rights reserved.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(){
    int n, m, k, q;
    // n 是柜台数,m 是每个柜台前可以容纳的等待顾客数,k 是顾客数,q 是询问的顾客数 

    scanf("%d %d %d %d", &n, &m, &k, &q);
    int hour = 8, minute = 0;

    vector<queue<vector<int>>> bank(n);
    queue<int> waiting; // int 表示顾客编号
    vector<vector<int>> customers(k, vector<int>(3, -1)); // [0] 表示需要服务的时间,[1] 表示结束服务的小时,[2] 表示结束服务的分钟

    for(int i = 0; i < k; ++i) scanf("%d", &customers[i][0]);

    int nowAt = 0; // 安排 8 点钟的时候柜台的顾客分布

    for(int i = 0; i < min(k, m * n); ++i){
        if(nowAt == n) nowAt = 0;

        bank[nowAt].push({i + 1, customers[i][0]});

        nowAt++;
    }

    if(k > n * m){
        for(int i = n * m; i < k; ++i) waiting.push(i + 1);
    }

    while(true){
        if(hour == 17){
            for(int i = 0; i < n; ++i){
                if(!bank[i].empty()){
                    int cus = bank[i].front()[0];
                    customers[cus - 1][1] = 17;
                    customers[cus - 1][2] = minute + bank[i].front()[1];
                }
            }

            break;
        }

        for(int i = 0; i < n; ++i){
            if(!bank[i].empty()){
                if(bank[i].front()[1] == 0){
                    int customer = bank[i].front()[0];

                    customers[customer - 1][1] = hour;
                    customers[customer - 1][2] = minute;
                    bank[i].pop();
                    
                    // 处理好出队的这位后看还有没有正在等待的顾客,有的话入队,这个时候需要考虑挑选最小的柜台编号入队么?
                    // 不用,因为我们保证了只要有人还在等待,那么每个柜台前一定是满的,并且我们是每一分钟都是从小到大遍历柜台,
                    // 那么当我们遍历到 i 柜台发现有出队后,之前的 i - 1 个柜台一定是满的
                    if(!waiting.empty()){
                        int customer = waiting.front();

                        waiting.pop();

                        bank[i].push({customer, customers[customer - 1][0]});
                        
                    }

                    if(!bank[i].empty()) bank[i].front()[1]--;
                }

                else bank[i].front()[1]--;
            }
        }

        hour = (minute == 59 ? hour + 1 : hour);
        minute = (minute == 59 ? 0 : minute + 1);
    }

    int toAsk = 0;
    for(int i = 0; i < q; ++i){
        scanf("%d", &toAsk);

        if(customers[toAsk - 1][1] > 0) printf("%02d:%02d\n", customers[toAsk - 1][1], customers[toAsk - 1][2]);
        else printf("Sorry\n");
    }

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

负反馈循环

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

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

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

打赏作者

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

抵扣说明:

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

余额充值