1017. Queueing at Bank (25)
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 (<=10000) - 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 10Sample Output:
8.2
每一个顾客是一个person结构体,里面有他来到银行的时间,等待的时间和接受服务的时间。
所有的顾客在customer数组里。
ToEight函数算的是到八点钟的时间。
把所有的顾客按照来的时间从早到晚排好序。
一共有w个窗口,用window数组表示,window[j]表示的是第i个窗口结束服务的时间。那么window[j]==i表示该窗口可以服务新客户了。注意此时要讨论队列里有人和队列里没有人。
pointer表示的是轮到第pointer个顾客进队了。
注意:当到5点时,如果队列里还有人,则继续排,但不接受新的顾客进队,即队列只出不进。
#include <stdio.h>
#include <algorithm>
#define MAX 10000
#define MAXWIN 100
struct person {
int hour, min, sec;
int processing;
int wait = 0;
};
person customer[MAX];
bool cmp(person a, person b);
int queue[MAX];
int ToEight(person a);
int window[MAXWIN] = { 0 };
int main()
{
int n, w;
int front = 0;
int rear = 0;
int pointer = 0;
scanf("%d %d", &n, &w);
for (int i = 0; i < n; i++) {
scanf("%d%*c%d%*c%d", &customer[i].hour, &customer[i].min, &customer[i].sec);
scanf("%d", &customer[i].processing);
}
std::sort(customer, customer + n, cmp);
for (int i = 0; i < n; i++) {
if (customer[i].hour < 8) {
queue[++rear] = i;
customer[i].wait = ToEight(customer[i]);
pointer++;
}
else
break;
}
for (int i = 0; i <= 9 * 60 * 60||front != rear; i++) {
for (int j = 0; j < w; j++) {
if (window[j] == i) {
if(front != rear)
window[j] = i + customer[queue[++front]].processing * 60;
else {
window[j]++;
}
}
}
for (int k = front + 1; k <= rear; k++) {
customer[queue[k]].wait++;
}
/*if (pointer == n)
break;*/
if (i <= 9 * 60 * 60 && i == ToEight(customer[pointer]) && pointer != n)
queue[++rear] = pointer++;
}
double sum = 0;
for (int i = 0; i < n; i++) {
sum += customer[i].wait;
}
printf("%.1f", sum / (pointer * 60));
return 0;
}
bool cmp(person a, person b)
{
if (a.hour != b.hour)
return a.hour < b.hour;
else if (a.min != b.min)
return a.min < b.min;
else
return a.sec < b.sec;
}
int ToEight(person a)
{
int hour, min, sec;
if (a.hour < 8) {
hour = 8 - a.hour - 1;
min = 60 - a.min - 1;
sec = 60 - a.sec;
}
else {
hour = a.hour - 8;
min = a.min;
sec = a.sec;
}
return hour * 60 * 60 + min * 60 + sec;
}