1014. Waiting in Line (30)

来源:互联网 发布:淘宝盗版软件举报 编辑:程序博客网 时间:2024/05/22 00:06

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.
Customer[i] will take T[i] 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 custmers 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

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

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个队列,按照要求进行入队出队,就是过程比较绕,但思路还是很清楚的。注意,有个陷阱是:在17:00前接受服务,但在17:00后完成服务的是可以的,即不能按照服务完成时间来判断。

#include<iostream>#include<queue>#include<iomanip>using namespace std;struct queue_matrix{    int cur;    queue<int>q;    queue_matrix(){ cur = 8 * 60; }//构造函数,初始化为8点钟}mqueue[21];struct customer{    int start_time;    int end_time;    int serve_time;}cust[1002];int main(){    int n, m, k, q;    cin >> n >> m>>k>>q;    int order=0;    for (int i = 0; i < k; i++)//读入k个人分别需要的服务时间        cin >> cust[i].serve_time;    int k_copy = k;    for (int i = 0; order < k&&order < n*m; order++, i = (i + 1) % n)//先站满队列    {        cust[order].start_time = mqueue[i].cur;        mqueue[i].cur += cust[order].serve_time;        mqueue[i].q.push(order);        cust[order].end_time = mqueue[i].cur;    }    /*for (int i = 0; i < order; i++)        cout << cust[i].end_time<< endl;        */    while (k_copy-->m*n)    {        int min_t = 100000;        int min_t_id = -1;        for (int i = 0; i < n; i++)//找出n个队列里面最先办完的人出队        {                //if (!mqueue[i].q.empty()) cout << "empty" <<i<< endl;            int temp = mqueue[i].q.front();            //if (!mqueue[i].q.empty()) cout << "empty" << i<<endl;            //cout << temp << endl;            if (min_t>cust[temp].end_time)            {                min_t = cust[temp].end_time;                min_t_id = i;            }        }      //cout << min_t_id << endl;        mqueue[min_t_id].q.pop();        //排在黄线外面的人入队        cust[order].start_time = mqueue[min_t_id].cur;        mqueue[min_t_id].q.push(order);        mqueue[min_t_id].cur += cust[order].serve_time;        cust[order++].end_time = mqueue[min_t_id].cur;    }     /*for (int i = 0; i < k; i++)        cout << cust[i].end_time << ",";        */    //读入查询的序列并打印出时间    int query;    int time;    for (int i = 0; i < q; i++)    {        cin >> query;        time = cust[query-1].end_time;        if (cust[query-1].start_time< 1020)        {            cout.fill('0');            cout << setw(2) << time / 60 << ":" << setw(2) << time % 60 << endl;        }        else            cout << "Sorry" << endl;    }}
0 0