【剑指紫金港】1014 Waiting in Line 排队模拟

A 1014 Waiting in Line

题目链接

Problem Description

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 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, customer​1 is served at window​1 while customer2​​ 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

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:MMwhere HHis in [08, 17] and MMis 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 Sorryinstead.

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个人,所有人都可以理解为在8:00到达,然后k个人依次按顺序循环排队,直到排满n个窗口的n*m个位置,剩下的人站在黄线外等待,哪个窗口的人先办完事情先到哪个窗口补队。最后是Q个询问,如果顾客在17:00(不含)之前开始在窗口办事,输出该顾客结束办事的时间,否则输出"Sorry"。

解题思路

先填满n*m个位置,每个新入队列(最大长度为m)的顾客的结束时间是队尾顾客的结束时间加上该顾客的办事时间。之后是黄线外依次进入黄线内,每进一个人遍历一次所有窗口,选出最早办完事的窗口(窗口队列的队首顾客结束时间),然后将下一个人安排进该窗口队列队尾。

AC代码

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f

const int start = 8*60; //开始时间8:00
const int last = 17*60; //结束时间17:00
const int maxn = 1005;

struct node{
    int id;  //顾客编号
    int t;  //办事结束时间
};

int ans[maxn],n,m,k,q,ask;
queue<node> win[21]; //记录窗口情况

int main(){
    scanf("%d%d%d%d",&n,&m,&k,&q);
    fill(ans,ans+maxn,-1);
    vector<int> man(k+1);
    for(int i=1;i<=k;i++){
        scanf("%d",&man[i]);
    }
    int num=1;
    int stop=n*m;
    while(num<=stop){
        if(num>k){
            break;
        }
        int local = num%n==0 ? n : num%n; //当前窗口号
        if(num<=n){
            win[local].push({num,man[num]+start});
            ans[num]=man[num]+start;
        }else{
            int st=win[local].back().t; //该窗口队伍最后一个人的结束时间
            int res=man[num]+st;
            win[local].push({num,res});
            if(st<last){
                ans[num]=res;
            }
        }
        num++;
    }
    for(;num<=k;num++){
        int mint=INF,minn;
        for(int i=1;i<=n;i++){
            if(!win[i].empty()){
                if(win[i].front().t<mint){
                    mint=win[i].front().t; //窗口正在办事的人的结束时间
                    minn=i;
                }
            }
        }
        int st=win[minn].back().t;
        int res=man[num]+st;
        if(st<last){
            ans[num]=res;
        }
        win[minn].push({num,res});
        win[minn].pop();
    }
    while(q--){
        scanf("%d",&ask);
        if(ans[ask]==-1){
            printf("Sorry\n");
        }else{
            int HH=ans[ask]/60;
            int MM=ans[ask]%60;
            printf("%02d:%02d\n",HH,MM);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值