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

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

Input Specification:

Each input file contains one test case. Each case starts with a line containing positive integers: , number of windows), the maximum capacity of each line inside the yellow line), , number of customers), and , number of customer queries).

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

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

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

Solve

题目让解决顾客排队问题,要求输出每位顾客的服务结束时间,并且当开始时间不能在17点之前时输出 Sorry(结束时间可以晚于 17 点)。

我们定义任意一个顾客的结束时间为: 其中 是已知条件,那么需要确定

题目中存在 个窗口,每个窗口面前有能容纳 个人的排队区,而在排队区外还有被黄线分割的等候区。当窗口空闲时,顾客的选择策略为优先选择队列人数少的,当人数都一致时选择下标小的。

因此可知,当人数不超过排队区能容纳的最大人数,即 时,顾客是采用轮询方式均匀分配到窗口中,这一固定的分配策略使得我们可以用静态方式直接处理得到前 个顾客的开始和结束时间。

具体来说,当队列为空时,即最开始没有人来时候,由于顾客是同时在 到达,则队列中第一个来的人其开始时间为 ,第二个人开始时间为第一个人的结束时间,第三个人的开始时间为第二个人的结束时间…

以此类推,有如下策略:

由此便可确定前 个顾客的开始和结束时间,当然若实际人数不足以超过容纳人数,则直接就求得了最终结果。

接下来考虑超过的部分,此时由于上述的静态处理,每个队列中都被塞满了已经确定结束时间的顾客,那么首先根据顾客择队策略,要先找出最先出现空闲位置的队列将当前顾客放进去。

找出该队列,设为 ,之后,新加入的顾客需要确认其开始时间和结束时间,则其开始时间依然是 队尾顾客的结束时间,确定之后再将队头顾客出队并将新顾客入队以供下一个顾客确定其开始时间。

至于特殊情况,队列不可能为空,因为我们是在出队的同时一定有一个元素入队,并且只要能开始处理 顾客就一定意味着此时队列全满。

最后判断时间就直接全化为分钟来处理即可。

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
 
struct Customer {
    int startTime;
    int costTime;
    int endTime;
};
 
int main() {
    int n, m, k, q;
    cin >> n >> m >> k >> q;
    vector<Customer> customers(k + 1);
    for(int i = 1; i <= k; i++) {
        auto& t = customers[i];
        cin >> t.costTime;
        t.startTime = -1;
        t.endTime = -1;
    }
 
    // 先处理 (n*m) 的,可以直接确定每个顾客的结束时间
    vector<queue<Customer>> windows(n);
    for(int i = 1; i <=  min(k, n * m); i++) {
        int windowId = (i - 1) % n;
        auto& customer = customers[i];
 
        if(i <= n)
            customer.startTime = 0;
        else {
            Customer last = windows[windowId].back();
            customer.startTime = last.endTime;
        }
 
        customer.endTime = customer.startTime + customer.costTime;
        windows[windowId].push(customer);
    }
 
    // 后续的需要动态处理,每个顾客的开始时间要基于当时的最小可入队列的末尾顾客的结束时间
    for(int i = n * m + 1; i <= k; i++) {
        auto& customer = customers[i];
        int earlyWindow = 0, earlyTime = 0x3f3f3f3f;
        for(int j = 0; j < n; j++) {
            if(windows[j].empty() || earlyTime > windows[j].front().endTime)
                earlyWindow = j, earlyTime = windows[j].front().endTime;
        }
 
        if(windows[earlyWindow].empty())
            customer.startTime = 0;
        else {
            Customer last = windows[earlyWindow].back();
            customer.startTime = last.endTime;
        }
 
        windows[earlyWindow].pop();
        customer.endTime = customer.startTime + customer.costTime;
        windows[earlyWindow].push(customer);
    }
 
    while(q--) {
        int queryId;
        cin >> queryId;
 
        auto& customer = customers[queryId];
        if(customer.startTime >= 540)
            cout << "Sorry" << endl;
        else
            printf("%02d:%02d\n", (480 + customer.endTime) / 60, customer.endTime % 60);
    }
    
    return 0;
}