1017 Queueing at Bank (25 分)
Abstract
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.
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
Note
- 题意: n个客户,k个窗口。有客服务窗口就去办理业务
- 思路: 利用windows数组记录每个窗口下一次可以工作的时间,每次更新该值
- 错误:
- duration忘记转换成秒, 错了好长时间, 10 ^ 4 是10010, 少写一个0 导致最后一个测试点错误。
- 机房的codeblocks还不会debug, 肉眼太费时间
- 用时1小时。。。。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 11000
struct customer{
int arrive_time;
int duration;
}a[MAX];
bool cmp(customer a, customer b){
return a.arrive_time < b.arrive_time;
}
int main() {
int num, windows, cnt = 0, waiting_sum = 0;
cin >> num >> windows;
for(int i = 0; i < num; i++){
int hh, mm, ss, during;
scanf("%d:%d:%d %d", &hh, &mm, &ss, &during);
if(!(hh >= 17 && mm + ss > 0)) {
a[i].arrive_time = hh * 3600 + mm * 60 + ss;
a[i].duration = during * 60;
cnt++;
}
else a[i].arrive_time = 72000;
}
if(cnt == 0){
cout << "0.0";
return 0;
}
// cout << cnt << endl;
vector<int> window(windows, 28800);
sort(a, a + num, cmp);
for(int i = 0; i < cnt; i++){
//cout << a[i].arrive_time << " " << a[i].duration << endl;
int Min = 0, mintime = window[0];
for(int j = 0; j < windows; j++){
if(window[j] < mintime){
mintime = window[j];
Min = j;
}
//cout << window[j] << " ";
}
//cout << window[Min]<< endl;
if(a[i].arrive_time < 28800){ waiting_sum += 28800 - a[i].arrive_time; a[i].arrive_time = 28800; }
if(a[i].arrive_time < window[Min]){
waiting_sum += window[Min] - a[i].arrive_time;
window[Min] += a[i].duration;
}
else window[Min] = a[i].arrive_time + a[i].duration;
}
// cout <<waiting_sum << endl;
// printf("%.1f\n", waiting_sum / 60.0 / cnt);
printf("%.1lf", (double)(waiting_sum * 1.0 / (cnt * 60)));
}