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.
  • Customeri 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, 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 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

思路(参考柳婼)

  • 用结构体存放每个窗口的第一个顾客的出队时间poptime和最后一个顾客弄完业务的时间endtime,poptime的作用就是当顾客办完业务,从窗口退出,通过比较每个窗口的poptime即可知道在黄线外等待的顾客去哪个窗口;endtime的作用就是判断新的顾客加入到窗口会不会被sorry,如果endtime<540(9小时),说明下一个顾客加进去也不会被sorry,即使这个顾客完成业务的时间超过了540,也不会被sorry。而如果窗口的endtime>=540,那么新的顾客加进去,而工作人员已经下班,所以sorry。
  • res数组记录每个顾客完成业务的时间,这时间是指8:00到顾客完成业务所用的分钟数,最后输出的时候对res的数据进行处理,这样操作比较简便
  • c++控制输出:right:右对齐;setw(2):控制输出字符为2;setfill(‘0’)设置填充字符为0

Code

#include <iostream>
#include <queue>
#include <vector>
#include <iomanip>
using namespace std;

struct window_queue
{
    int poptime,endtime;
    queue<int>q;
};

int main(){
    int windows_sum,windows_capacity;
    int customers_sum,query_customers_sum;
    cin >> windows_sum>>windows_capacity>>customers_sum>>query_customers_sum;
    vector<window_queue>window(windows_sum+1);  // 窗口队列
    vector<int>times(customers_sum+1);  // 记录每个顾客完成业务需要的时间
    vector<int>res(customers_sum+1);  // 记录每个顾客完成业务的时间
    vector<bool>sorry(customers_sum+1, false);  // 记录每个顾客是否被sorry
    for(int i=1; i<=customers_sum; i++){
        cin>>times[i];
    }
    int index = 1;
    for(int i=1; i<=windows_capacity; i++){     // 将windows_sum*windows_capacity个顾客先进队,也就是将窗口的队列占满
        for(int j=1; j<=windows_sum; j++){
            if(index <= customers_sum){
                window[j].q.push(times[index]);
                if (window[j].endtime >=540){ //9*60=540
                    sorry[index] = true;
                }
                window[j].endtime += times[index];
                if (i==1){
                    window[j].poptime = window[j].endtime;
                }
                res[index] = window[j].endtime;  // res记录顾客办完业务的时间
                index++;
            }
        }
    }
    // 注意:我们是从1开始存放窗口的
    while (index <= customers_sum){  // 找个第一个办完业务的顾客所在的窗口,由新的顾客填入队列
        int first_poptime = window[1].poptime;
        int first_pop_window_index = 1;
        for(int i=2; i<=windows_sum; i++){  // 如果同时办完,选择窗口编号小的先填入新的顾客
            if (first_poptime > window[i].poptime){
                first_poptime = window[i].poptime;
                first_pop_window_index = i;
            }
        }
        // 找到新的顾客填入的窗口下标
        window[first_pop_window_index].q.pop();
        window[first_pop_window_index].q.push(times[index]);
        int front = window[first_pop_window_index].q.front();
        window[first_pop_window_index].poptime += front;
        if(window[first_pop_window_index].endtime>=540){
            sorry[index] = true;
        }
        window[first_pop_window_index].endtime += times[index];
        res[index] = window[first_pop_window_index].endtime;
        index++;
    }
    // for(int i=1; i<=customers_sum; i++){
    //     cout << res[i] << endl;
    // }
    int query_index;
    for (int i=1; i<=query_customers_sum; i++){
        cin >> query_index;
        if (sorry[query_index]){
            cout << "Sorry" << endl;
        }else{
            cout << setw(2) << right << setfill('0') << res[query_index]/60+8;
            cout << ":";
            cout << setw(2) <<right << setfill('0') << res[query_index]%60 <<endl;
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值