「1014」Waiting in Line

本文介绍了一种银行排队系统的策略,涉及窗口容量、顾客选择线程的规则,以及如何根据处理时间预测交易完成时间。通过算法解决顾客何时能完成业务的问题,包括黄线内外的排队管理和优先级分配。

ENTRY

Suppose a bank has 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 customers. Hence when all the lines are full, all the customers after (and including) the 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.
  • will take minutes to have his/her transaction processed.
  • The first customers are assumed to be served at 8:00 am.

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, is served at while is served at . will wait in front of and will wait in front of . will wait behind the yellow line.

At 08:01, is done and enters the line in front of since that line seems shorter now. will leave at 08:02, at 08:06, at 08:07, and finally at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: (≤20, number of windows), (≤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

Ω

极简翻译:银行有 个窗口,每个窗口前最多排 个人,共有 个顾客,每个顾客的事务处理时间都是知道的,其中 个顾客来查询自己的结束时间。当窗口未排满的时候,顾客会前往人数最少的窗口,若有多个人数最少的窗口则去编号最小的;当所有窗口都排满时,后面的顾客需等在黄线后直到有顾客完成离开窗口。

很显然,本题考查的是队列queue,然后考虑的东西有一点多:

  1. 位顾客按窗口编号从小到大依次排列,不考虑处理时间
  2. 后的顾客则看哪个窗口的第一个(⚠️不是最后一个)顾客最先完成事务就去哪个
  3. 顾客数量并不一定会完全填充满窗口
  4. 同一时刻如果有多个窗口空出来黄线外的第一位顾客前往编号最小的窗口
  5. 若顾客开始处理事务的时间(⚠️不是处理完成时间)≥17:00,那么不受理该顾客的事务

因此我们需要用队列存储每个窗口排队的客户编号来获知每个时刻窗口第一个顾客是谁,另外当我们把一个顾客分配至一个窗口时该顾客的结束时间便已经确定。对于黄线外第一位顾客的窗口选择问题,我们只需要比较当前每个窗口的第一位顾客结束时间,然后分配至结束时间最早的窗口即可。


C☺DE

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int main()
{
    int n, m, k, q, t, c;
    cin >> n >> m >> k >> q;
  //proc_time:每个顾客的事务所需时间,end_time:每个顾客的事务完成时间
  //line_time:每个窗口最后一位顾客的结束时间,fill_time:每个窗口当前第一位顾客的结束时间
  vector<int> proc_time(k), end_time(k), line_time(n, 480), fill_time(n, 0);
    for (int i = 0; i < k; ++i)
    {
        cin >> t;
        proc_time[i] = t;
    }
  //黄线内的实际顾客数
    int inline_num = min(n * m, k);
  //每个窗口的排队队列
    vector<queue<int>> line(n);
  //黄线内顾客的窗口分配
    for (int i = 0; i < inline_num; ++i)
    {
        line[i % n].push(i);
        end_time[i] = line_time[i % n] += proc_time[i];
    }
  //如果每个窗口都满了(k≥mn)
    if (inline_num < k)
    {
      //初始化每个窗口第一位顾客的结束时间
        for (int i = 0; i < n; ++i)
        {
            fill_time[i] += proc_time[line[i].front()];
            line[i].pop();
        }
      //分配黄线外的顾客
        for (int i = inline_num; i < k; ++i)
        {
          //求最早空出位置的窗口编号
            int win = int(min_element(fill_time.begin(), fill_time.end()) - fill_time.begin());
            end_time[i] = line_time[win] += proc_time[i];
            fill_time[win] += proc_time[line[win].front()];
            line[win].pop();
            line[win].push(i);
        }
    }
    for (int i = 0; i < q; ++i)
    {
        cin >> c;
      //考虑每个顾客开始处理的时间=结束时间-处理时间
        if (end_time[c - 1] - proc_time[c - 1] >= 1020)
            cout << "Sorry" << endl;
        else
            printf("%02d:%02d\n", end_time[c - 1] / 60, end_time[c - 1] % 60);
    }
}

⚡︎ 算法说明

  1. 所有时间都先按分钟计算,最后转为时间
  2. 给黄线外的第一个顾客分配完窗口后,需要更新该窗口第一个顾客的结束时间
  3. min_element(fill_time.begin(), fill_time.end())找到的是最左边最小值的迭代器,因此符合当有多个最小人数窗口时选择编号最小的要求
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值