PAT_A 1014. Waiting in Line (30)

来源:互联网 发布:如何找淘宝客推广 编辑:程序博客网 时间:2024/06/05 05:41

1014. Waiting in Line (30)

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

  • 分析:

    • 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.
      这条规则是,N×M之前的人站在各个窗口的队列里,如何选择队列,并没有说,我们就按窗口的编号从小到大排(也必须按这样方式安排前M*N个)。MxN之后的人排成一队,选队列规则在第二条rule里。
    • 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.
      每个客户处理的时间已给,并且第一批窗口处理用户的开始时间为8:00
    • 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.
      输出规则:在17:00之后不可申请服务。但在17:00之前申请的服务,允许服务结束时间超过17:00(最开时,我一直按结束时间算的,错了三个测试点)
    • 题目挺简单的,但是得注意题目要求。
  • code:

#include<iostream>#include<queue>#include<vector>#include<cstdio>using namespace std;int N,M,K,Q;int pre[22];//用来记录每个窗口从8:00到现在的时间差int sum[22];//记录用户处理完所花时间queue<int>que[22];//使用队列最好的场景queue<int>qwait[22];//M*N之后的用户vector<int>out;//所问用户所花时间记录int main(){    cin>>N>>M>>K>>Q;    int tmp;    for(int i=0;i<K;i++)    {        cin>>tmp;        if(i<M*N)        {            que[i%N].push(tmp);            //窗口有空闲时也得考虑17:00之前能否接受服务            if(sum[i%N]>=540)                out.push_back(-1);            else                out.push_back(sum[i%N]+tmp);            sum[i%N]+=tmp;        }else        {                int min=que[0].front()+pre[0];                int p=0;                //选择最先空闲的窗口                for(int j=0;j<N;j++)                {                    //pre为从8:00开始到前一个用户结束的总时间差                    //加上当前用户所需时间,即可选出最先结束服务的窗口                    if(min>que[j].front()+pre[j])                    {                        min=que[j].front()+pre[j];                        p=j;                    }                }                //用于选择窗口                //处理结束的用户                pre[p]=min;                que[p].pop();                //入队                que[p].push(tmp);                //17:00之前能否接受服务                if(sum[p]>=540)                    out.push_back(-1);                else                    //更新当前用户完成服务,距离8:00的总时间差                    out.push_back(sum[p]+tmp);                sum[p]+=tmp;//计算完成时间        }    }    for(int i=0;i<Q;i++)    {        cin>>tmp;        tmp=out[tmp-1];        if(tmp==-1)        {            printf("Sorry\n");            continue;        }        printf("%02d:%02d\n",8+tmp/60,tmp%60);    }    return 0;}

1014

0 0
原创粉丝点击