【PAT】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 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.
翻译:假设一个银行开放N个窗口用于办理服务。在窗口前有条黄线将等待区划分为两个部分。在线内的顾客规则如下:
每个窗口前面的黄线可以供M个客户排队。因此,当所有N行都满时,所有后面的客户(和包括)(NM + 1)将不得不排在黄线后面。
每个客户在穿过黄线时将选择最短的行来等待。如果有两队及以上长度相同,客户将始终选择数字最小的窗口。
客户[i]将在T [i]分钟内办理他/她的服务。
前N名客户假定在上午8:00到达银行。
现在给予每个客户的服务时间,您应该告诉客户完成业务的确切时间。

例如,假设一个银行有2个窗口,每个窗口最多有两个人在黄线内等待。分别5个需要1,2,6,4,3分钟服务客户在等待服务。在上午08:00,客户1在窗口1办理服务,而客户2在窗口2办理服务。
客户3将在窗口1之前等待,客户4将在窗口2之前等待。客户5将等待在黄线后面。

在08:01,客户1完成,客户5进入窗口1之前的行,因为这条线似乎更短。客户2将在08:02完成,客户4在08:06完成,客户3将在08:07完成,最后客户5在08:10完成。

INPUT FORMAT

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.

翻译:每个测试文件包含一组测试数据。每组测试数据开头第一行包括4个正整数:N (<=20, 窗口数), M (<=10,黄线前最多可排队的人数), K (<=1000, 客户数), and Q (<=1000, 查询的客户数)。

接下来的一行包括K个正整数,代表K个客户各自的花费时间。

最后一行包括Q个 正整数,代表那些询问自己合适可以完成服务的客户。客户被标记为1到K。

OUTPUT FORMAT

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.

翻译:对于每个客户,在一行内输出他的业务办理什么完成,以HH:MM的格式,HH为 [08, 17],MM为 [00, 59],注意由于银行每天下午5点之后关门,所以如果顾客没能在17:00前被服务,你必须输出“Sorry”。


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


解题思路:

这道题用队列解决即可,用优先队列保存每个窗口正在办理业务的人,再用队列保存每个窗口正在排队的人。这道题的注意点为只有在5:00之前没有开始办理服务的人才输出sorry,这个很重要。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue> 
#include<algorithm>
#define INF 99999999
using namespace std;
typedef pair<int,int> P;
int N,M,K,Q,people[1010],Judge[1010],NowTime[25];

struct window{
    int num,pnum,time;
    window(int a,int b,int c):num(a),pnum(b),time(c){}
    bool operator <(const window &a)const{
        return a.time==time?num>a.num:time>a.time;
    }
};
queue<P> q[25];
priority_queue<window> pq;

void fun(){
    for(int i=0;i<N;i++){       
        if(!q[i].empty()){
            P temp=q[i].front();q[i].pop();
            pq.push(window(i,temp.first,temp.second));
        }
    }
    int ccount=N*M;
    while(!pq.empty()){
        window tempWin=pq.top();pq.pop();
        people[tempWin.pnum]=tempWin.time;
        if(ccount<K){
            if(NowTime[tempWin.num]>=9*60)
            q[tempWin.num].push(P(ccount,-1));
            else
            q[tempWin.num].push(P(ccount,people[ccount]+NowTime[tempWin.num])),NowTime[tempWin.num]+=people[ccount];
            ccount++;
        }
        if(!q[tempWin.num].empty()){
            P temp=q[tempWin.num].front();q[tempWin.num].pop();
            pq.push(window(tempWin.num,temp.first,temp.second));
        }
    }
}
void print(int a){
    int Time=people[a];
    if(Time==-1)printf("Sorry\n");
    else printf("%02d:%02d\n",Time/60+8,Time%60);
}
int main(){
    scanf("%d%d%d%d",&N,&M,&K,&Q);
    for(int i=0;i<K;i++){
        scanf("%d",&people[i]);
        if(i<N*M){
            if(NowTime[i%N]>=9*60)
            q[i%N].push(P(i,-1));
            else
            q[i%N].push(P(i,people[i]+NowTime[i%N])),NowTime[i%N]+=people[i];
        }
    }
    for(int i=0;i<Q;i++)scanf("%d",&Judge[i]);
    fun();
    for(int i=0;i<Q;i++)print(Judge[i]-1);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值