Queueing at Bank
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 10
Sample Output:
8.2
考点
- 模拟逻辑
题解(时间复杂度为O(NK))
- 先对输入的信息进行sort,设置windows的时间为8:00。
- 外则for循环是customer,内层是windows。内层for循环找到最先服务完的window,将window完成的时间和队首的到达时间对比,选择较大的一个做为此window下一个服务的时间。
- 如果当前队首到达时间大于17:00,则break。
- 更新总服务时间以及服务人数。
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <stdio.h>
using namespace std;
#define MAXSIZE (10000+10)
#define INF 0x7fffffff
int window[100+1];
struct node{
int arrive;
int serve;
}person[MAXSIZE];
bool cmp(node a,node b){
return a.arrive<b.arrive;
}
int main(){
//freopen("./in","r",stdin);
int n,k;
scanf("%d%d",&n,&k);
int i,j;
int h,m,s,w;
for(i=0;i<n;i++){
scanf("%d:%d:%d %d",&h,&m,&s,&w);
person[i].arrive=h*60*60+m*60+s;
person[i].serve=w*60;
}
sort(person,person+n,cmp);
fill(window,window+k,8*60*60);
int total=0;
int early_window,flag;
int cnt=0;
for(i=0;i<n;i++){
early_window=INF;
for(j=0;j<k;j++){
if(early_window>window[j]){
early_window=window[j];
flag=j;
}
}
if(person[i].arrive>17*60*60)break;
early_window=max(early_window,person[i].arrive);
cnt++;
total+=early_window-person[i].arrive;
window[flag]=early_window+person[i].serve;
}
if(cnt==0)cnt=1;
printf("%.1f",total*1.0/cnt/60);
return 0;
}