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 (≤10^4) - 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.
按照到达时间排序,超过17:00的在输入时剔除,对于每一个人,选择最早空出来的窗口。
窗口可以使用优先队列优化。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct peo {
int arrive, process;
};
bool cmp(peo p1, peo p2) { return p1.arrive < p2.arrive; }
int main() {
int n, k, h, m, s, p;
float ans = 0;
cin >> n >> k;
vector<peo>v;
vector<int>time(k);
for (int i = 0; i < k; i++)time[i] = 8 * 3600;
for (int i = 0; i < n; i++) {
scanf("%d:%d:%d %d", &h, &m, &s, &p);
if (h * 3600 + m * 60 + s <= 17 * 3600)
v.push_back({ h * 3600 + m * 60 + s ,p * 60 });
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++) {
int early = time[0], pos = 0;
for (int j = 0; j < k; j++) {//最早空闲窗口
if (time[j] < early) {
early = time[j];
pos = j;
}
}
if (v[i].arrive < time[pos]) {//顾客到达时没有空闲窗口
ans += time[pos] - v[i].arrive;
time[pos] += v[i].process;
}
else //顾客到达时已经有空闲窗口,不需等待
time[pos] = v[i].arrive + v[i].process;
}
printf("%0.1f\n", ans / v.size() / 60);
return 0;
}