PAT 甲级 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.
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 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, customer
​1
​​ is served at window
​1
​​ while customer
​2
​​ is served at window
​2
​​ . Customer
​3
​​ will wait in front of window
​1
​​ and customer
​4
​​ will wait in front of window
​2
​​ . Customer
​5
​​ will wait behind the yellow line.

At 08:01, customer
​1
​​ is done and customer
​5
​​ enters the line in front of window
​1
​​ since that line seems shorter now. Customer
​2
​​ will leave at 08:02, customer
​4
​​ at 08:06, customer
​3
​​ at 08:07, and finally customer
​5
​​ 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个窗口,每个窗口可以排m人。有k为顾客需要办理业务,给出了每个客户的办理业务时间。
银行在8点开始服务,如果窗口都排满了,客户就得在黄线外等候。如果有一个窗口用户服务结束,
黄线外的客户就进来一个。如果有多个可选,选窗口id最小的。
输出查询客户的服务结束时间。

如果客户在17点(注意是包括的!!!就在这里被坑了,一开始还以为不包括。。。)或者以后还没开始服务,就输出Sorry
如果已经开始了,无论多长都会继续服务的。

代码

//https://blog.csdn.net/qq_24452475/article/details/99708103
#include<iostream>
#include<queue>
using namespace std;
int h2m(int h,int m)
{
    return h*60+m;
}
struct Window
{
    int endtime=h2m(8,0),poptime=endtime;
    queue<int> q;
}window[20];

int main()
{
    int N,M,K,Q;
    scanf("%d %d %d %d",&N,&M,&K,&Q);
    int wait[K],finish[K];
    for(int i=0;i<K;i++)
    {
        cin>>wait[i];
    }
    int index=0;
    for(int i=0;i<min(M*N,K);i++)
    {
        window[index%N].q.push(index);
        //窗口结束时间
        window[index%N].endtime+=wait[index];
        //更新首个客户的完成时间
        if(index<N) window[index].poptime=wait[index];
        finish[index] = window[index%N].endtime;
        index++;
    }
    for(;index<K;index++)
    {
        int idx=-1,minpoptime=1<<30;//一个较大的数
        for(int i=0;i<N;i++)
        {
            if(window[i].poptime<minpoptime)
            {
                idx=i;
                minpoptime=window[i].poptime;
            }
        }
        Window& W = window[idx];
        W.q.pop();
        W.q.push(index);
        //更新窗口队列的endtime
        W.endtime+=wait[index];
        //更新窗口的poptime
        W.poptime+=wait[W.q.front()];
        //index服务结束时间为窗口的endtime
        finish[index]=W.endtime;
    }
    int id;
    for(int i=0;i<Q;i++)
    {
        scanf("%d",&id);
        if(finish[id-1]-wait[id-1]>=h2m(17,0)) printf("Sorry");
        else
        {
            printf("%02d:%02d",finish[id-1]/60,finish[id-1]%60);
        }
        if(i<Q-1) printf("\n");
           
    }
    system("pause");
}

思路

建立一个优先级队列,存储在黄线之内的所有客户。
对于min(m*n,K)之前的人,依此往窗口里排队(即优先级队列里)即可。
对于之后的人,从优先级队列里取出结束时间最少的客户,有相同的话则是窗口最小的,
进入他所在的队列。
同时还要建立linetime数组,存储每个窗口排在末尾客户的结束时间。
当窗口j新加进来一个客户,结束时间则是开始时间+服务时间。

最后对于查询的客户,如果起始时间>=17:00,则输出Sorry
否则输出对应的结束时间即可,这里方便起见,以分数存储的,所以最后还要转化一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值