「1017」Queueing at Bank

ENTRY

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2

Ω

本题算是低配版「1014」Waiting in Line,区别在于每个窗口前不能排队,所有等候的人都在黄线外排成一排,当有窗口空了才能进入。

极简翻译:银行有 个窗口,每个窗口接待一人。顾客按照到来的先后顺序在黄线外排成一排,当有窗口空闲时依次进入。我们已知每个顾客到达的时间,以及事务所需的处理时间,最终输出所有顾客的平均等待时间。注意,银行营业时间为8:00-17:00,17:00后到达的顾客不计入平均等待时间。

这一题相比1014事实上退化了很多,一方面是每个窗口前不能排队,所有人排在一个队伍里;另一方面,最后需要输出的结果并不是针对具体的个体,而是刻画整体的平均等待时间。因此我们也无需给每一个顾客进行编号,直接用pair存储到达时间和处理时长即可:

# pair<arrival time in second, processing time in second>
vector<pair<int,int>> info;

注意到,顾客信息不是按照到达顺序给出的,因此我们需要先对所有顾客的到达时间进行sort排序,然后遍历每一位顾客,选择时间轴最早的窗口,计算相应的等待时间,同时将顾客的处理时长加到该窗口的时间轴上。当循环到某位顾客的到达时间晚于17:00时则break退出循环,并记录有效顾客数rn


C☺DE

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

using namespace std;
typedef pair<int, int> ptt;

int main()
{
    int n, k, h, m, s, p, rn, sum = 0;
    cin >> n >> k;
    vector<ptt> info;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d:%d:%d %d", &h, &m, &s, &p);
        info.push_back(move(ptt(3600 * h + 60 * m + s, p * 60)));
    }
    sort(info.begin(), info.end(), [](const ptt &a, const ptt &b) { return a.first < b.first; });
    vector<int> windows(k, 8 * 3600);
    for (rn = 0; rn < n; ++rn)
    {
        if (info[rn].first > 17 * 3600)
            break;
      # Count waiting time
        sum += (windows[0] > info[rn].first ? (windows[0] - info[rn].first) : (windows[0] = info[rn].first, 0));
      # Add processing time to time line
        windows[0] += info[rn].second;
        sort(windows.begin(), windows.end());
    }
    printf("%.1f", (sum % 60 / 60.0 + sum / 60) / rn);
}

⚡︎ 一些说明

  1. 所有时间都换算成秒

  2. 窗口的初始时间轴都设置为8:00就无需考虑早到的情况了

    vector<int> windows(k, 8 * 3600);
  3. 当出现窗口空闲而无顾客的时候,需要先将窗口的时间轴拉到顾客到达时间再加上事务处理时长

    (windows[0] = info[rn].first, 0) # 逗号表达式返回后者的值
  4. 每次处理完一个顾客都需要对所有窗口的时间线进行排序,那么windows[0]即为最早空闲窗口。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值