【PAT甲级A1014】Waiting in Line (30分)(c++)

1014 Waiting in Line (30分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

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 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

题意:

窗口排队处理业务,每个窗口可排m人,共n个窗口,等排队区间(最多mn人)有空位时,即办理好一个人后,第nm+1人进入区间排队。输出指定人最后处理完业务的时间。

思路:

1.分两段,先把k人填充进n*m的排队区间,此时每个窗口所对应所需记录两个指标,一个是窗口处理完所有排队人业务的时间endtime,一个是处理完当前排队的人业务的时间fronttime。在填充时,一行一行填充,每个窗口的endtime就是该排队人的结束业务时间;填充第一行时的fronttime就是endtime
2.计算所有窗口fronttime的最小值,就是最快完成业务的那个窗口,那个窗口需要去掉已处理业务的人,加入新排队的人,更新fronttime,更新endtime,此时的endtime就是新排队人的结束时间。
3.需要注意的是,判断是否sorry,不管是填充还是插入人之后,先判断当前是否属于业务时间,即未更新的endtime,然后再更新endtime(注意:只要是在业务时间内办理,就可以,如:16:59时办理500分钟,都可以!)

参考代码:

#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
struct window{
    int fronttime,endtime;
    queue<int> q;
};
bool cmp(window a,window b){
    return a.fronttime<b.fronttime;
}
int main() {
    int n,m,k,q,maxt=540,index=1;
    scanf("%d%d%d%d",&n,&m,&k,&q);
    int time[k+1],result[k+1],customer;
    vector<window> w(n+1);
    vector<bool> flag(k+1,false);
    for(int i=1;i<=k;i++){
        scanf("%d",&time[i]);
    }
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(index<=k){
                w[j].q.push(time[index]);
                flag[index]=w[j].endtime>=maxt?true:false;
                w[j].endtime+=time[index];
                if(i==1)w[j].fronttime=w[j].endtime;
                result[index++]=w[j].endtime;
            }
        }
    }
    while(index<=k){
        int temp=min_element(w.begin()+1,w.end(),cmp)-w.begin();
        w[temp].q.pop();
        w[temp].q.push(time[index]);
        w[temp].fronttime+=w[temp].q.front();
        flag[index]=w[temp].endtime>=maxt?true:false;
        w[temp].endtime+=time[index];
        result[index++]=w[temp].endtime;
    }
    for(int i=1;i<=q;i++){
        scanf("%d",&customer);
        if(!flag[customer])
            printf("%02d:%02d\n",(result[customer]+480)/60,(result[customer]+480)%60);
        else printf("Sorry\n");
    }
    return 0;
}

如有错误,欢迎指正

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值