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

#include<iostream>
#include<algorithm>
#include<iomanip>
#include <ctype.h>
#include<string.h>
#include<string>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<queue>
using namespace std;

struct Line {
    int firstendtime = 0;//排第一的人结束时间
    int endtime=0;//记录队尾时间
    queue<int> in_line;//记录队伍中人的服务时间
    //int per = in_line.size();//计算队伍人数
};

int N;//窗口数量<20
int M;//黄线内每行最多排队人数
int K;//客户人数
int Q;//需统计q个人服务结束时间
//服务时间8:00-17:00,共540min
int serve_time[1010];//记录所有人服务时间
int serve_end[1010];//需要具体统计的人
int serve_Q[1010];//每个人对应的结束时间
int main() {
    cin >> N >> M >> K >> Q;
    Line bank[21];//银行20行,从1开始计数
    for (int i = 1; i <= K; i++) {
        cin >> serve_time[i];
    }
    for (int i = 1; i <= Q; i++) {
        cin >> serve_end[i];
    }
    //初始化
    int index = 1;//统计排队人数量
    for (int i = 1; i <= M; i++) {
        for (int j = 1; j <= N; j++) {
            if (index <= K) {
                bank[j].in_line.push(serve_time[index]);
                if (bank[j].endtime >= 540) {//如果已经开始服务,则可以超过17:00
                    serve_Q[index] = -1;//-1表示sorry
                }
                else
                {
                    serve_Q[index] = bank[j].endtime+ serve_time[index];
                }
                bank[j].endtime = bank[j].endtime + serve_time[index];

                if (i == 1) {
                    bank[j].firstendtime = serve_time[index];
                }
                index++;
            }
        }
    }

    //开始进入黄线
    while (index <= K) {
        //判断最先结束的人出列
        int pop_per = 1000;
        int win_num = 0;
        for (int i = 1; i <= N; i++) {
            if (bank[i].firstendtime < pop_per) {
                pop_per = bank[i].firstendtime;
                win_num = i;
            }
        }

        //更新队伍

        bank[win_num].in_line.pop();
        bank[win_num].in_line.push(serve_time[index]);
        bank[win_num].firstendtime += bank[win_num].in_line.front();


        if (bank[win_num].endtime >= 540) {
            serve_Q[index] = -1;
        }
        else {
            serve_Q[index] = bank[win_num].endtime + serve_time[index];
        }
        bank[win_num].endtime += serve_time[index];
        index++;
    }
    for (int i = 1; i <= Q; i++) {
        if (serve_Q[serve_end[i]] == -1)cout << "Sorry" << endl;
        else {
            int hour = serve_Q[serve_end[i]] / 60;
            int min = serve_Q[serve_end[i]] % 60;
            cout.width(2);
            cout.fill('0');
            cout << hour + 8 << ':';
            cout.width(2);
            cout.fill('0');
            cout << min << endl;
        }
    }
}