PAT甲级(Advanced Level) Practice 1014 Waiting in Line

原题

Suppose a bank has 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 customers. Hence when all the N lines are full, all the customers after (and including) the (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.
  • Customeri​ will take Ti​ minutes to have his/her transaction processed.
  • The first 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, customer1​ is served at window1​ while customer2​ is served at window2​. Customer3​ will wait in front of window1​ and customer4​ will wait in front of window2​. Customer5​ will wait behind the yellow line.

At 08:01, customer1​ is done and customer5​ enters the line in front of window1​ since that line seems shorter now. Customer2​ will leave at 08:02, customer4​ at 08:06, customer3​ at 08:07, and finally customer5​ at 08:10.

Input Specification:

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

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

The last line contains 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.

Output Specification:

For each of the 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

题目翻译

假设一家银行开设了 N 个服务窗口。窗口前有一条黄线,将等候区分成两部分。客户排队等候的规则是:

1.每个窗口前黄线内的空间足以容纳 M 个客户排队。因此,当所有 N 条队伍都排满时,第 (NM+1)st 位之后(包括第 (NM+1)st 位)的所有顾客都必须在黄线后面排队等候。
2.每个顾客在穿过黄线时都会选择最短的队伍等候。如果有两条或两条以上长度相同的队伍,顾客总是选择人数最少的窗口。
3.顾客需要 Ti 分钟才能完成交易。
4.假定前 N 位顾客在上午 8:00 服务。

现在,给定每个客户的处理时间,你应该知道客户完成业务的确切时间。

输入规范:

每个输入文件包含一个测试用例。每个案例以一行开头,包含 4 个正整数: N(≤20,窗口数)、M(≤10,黄线内每行的最大容量)、K(≤1000,客户数)和 Q(≤1000,客户查询数)。

下一行包含 K 个正整数,即 K 个客户的处理时间。

最后一行包含 Q 个正整数,代表询问交易完成时间的客户。客户编号从 1 到 K。

输出规范:

对于 Q 位客户中的每一位,在一行中打印其交易完成的时间,格式为 HH:MM,其中 HH 在 [08, 17] 内,MM 在 [00, 59] 内。请注意,由于银行每天 17:00 后关闭,对于那些在 17:00 之前无法服务的客户,您必须输出 "sorry"。

解题思路

本题看起来比较复杂,其实把所有要做的事先列出来会发现并没有什么复杂的算法知识。

每个顾客都是先进先出,刚好对应了队列,可以用队列存储每个柜台的人数情况。要做的事情就是:

1.确定人数最少的队伍,并把一个队伍外的人加入该队列。

2.如果所有队列都是满人,找到最快结束的队列,并弹出队头。

注意所有人必须在17:00前开始办业务,所以需要记录每个人开始办业务的时间和结束办业务的时间,同一个队列中一个人结束的时间恰好是下一个人开始的时间。

代码(c++)

#include <bits/stdc++.h>
#include <queue>

using namespace std;

const int N = 30, M = 1010;

int n, m, k, cnt;
queue<int> q[N];     // 记录每个柜台人数数据
int fin[M], beg[M];  // 存每个人的起始时间和结束时间

int main(){
    cin >> n >> m >> k >> cnt;
    for(int i = 1; i <= k; i++) {
        int t;
        cin >> t;
        
        int shortq = 1;
        for(int j = 2; j <= n; j++)                        // 找到人数最少的队列
            if(q[j].size() < q[shortq].size())
                shortq = j;

        if(q[shortq].size() == m)                          // 所有窗口都排满了,找出最先服务完成的窗口
            for(int j = 2; j <= n; j++)
                if(q[j].front() < q[shortq].front())
                    shortq = j;

        beg[i] = (q[shortq].size() ? q[shortq].back() : 0);  // 开始时间为上一位的结束时间
        if(q[shortq].size() >= m)                            // 拿到开始时间后才能出队
            q[shortq].pop();
        fin[i] = beg[i] + t;
        q[shortq].push(fin[i]);
    }

    while(cnt--){
        int x;
        cin >> x;
        
        if(beg[x] >= 540) printf("Sorry\n");                  // 开始时间不晚于17点,而不是结束时间
        else printf("%02d:%02d\n", (8 + fin[x] / 60), (fin[x] % 60));
    }

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值