PAT(模拟)——1017 Queueing at Bank (25 分)
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 (≤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
题目大意:
排队问题,有k个窗口,给出n个顾客的到达时间和办理业务的时间,所有的顾客按照到达的先后顺序排成一队,当有窗户空闲,最前面的顾客上前办理。求顾客平均等待时间。注意:银行8点开门,17点之后到达的人不能办理,不计入总时间,
题目解析:
结构体node记录顾客到达时间和处理时间,用window[k]数组记录窗口办理业务的完成时间,初值设为8点。顾客按照到达时间从小到大遍历,每次选出最早办理完成的窗口,如果窗口完成时间早于顾客到达时间,总等待时间增加差值,更新窗口完成时间;反之只用更新窗口完成时间。
具体代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct node {
int come_time;
int process_time;
}customer[10010];
int window[110];
bool cmp(node x,node y){
return x.come_time<y.come_time;
}
int main() {
int n,k;
cin>>n>>k;
int hh,mm,ss,process_time;
for(int i=0; i<n; i++) {
scanf("%d:%d:%d %d\n",&hh,&mm,&ss,&process_time);
customer[i].come_time=hh*3600+mm*60+ss;
customer[i].process_time=process_time*60;
}
for(int i=0;i<k;i++)
window[i]=28800;
sort(customer,customer+n,cmp);
int count_time=0,count_num=0;
for(int i=0;i<n;i++){
if(customer[i].come_time<=61200){
int min_index=0,min_time=window[0];
for(int j=1;j<k;j++)
if(window[j]<min_time){
min_time=window[j];
min_index=j;
}
if(customer[i].come_time<min_time){
count_time+=min_time-customer[i].come_time;
window[min_index]+=customer[i].process_time;
}else{
window[min_index]=customer[i].come_time+customer[i].process_time;
}
count_num++;
}
}
if(count_num==0)
printf("0.0");
else
printf("%.1f",(double)count_time/count_num/60);
return 0;
}