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);
}
⚡︎ 一些说明
所有时间都换算成秒
窗口的初始时间轴都设置为8:00就无需考虑早到的情况了
vector<int> windows(k, 8 * 3600);
当出现窗口空闲而无顾客的时候,需要先将窗口的时间轴拉到顾客到达时间再加上事务处理时长
(windows[0] = info[rn].first, 0) # 逗号表达式返回后者的值
每次处理完一个顾客都需要对所有窗口的时间线进行排序,那么
windows[0]
即为最早空闲窗口。