PAT 1017. Queueing at Bank
PAT 1017. 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
分析
首先为窗户们创建一个数组,里面储存下一次服务的时间,初始化为8点,让后遍历排完序的合法输入即到达时间小于等于17点的,然后每次遍历过程中,挑选出窗户中可以最早服务的作为对象的服务窗口,若窗口的服务时间比对象到达时间晚,则等待时间要加上两者之差,并且该窗口的最早服务时间加上该对象的processing time;但若窗口的服务时间比对象早,说明不需要等待,直接服务,但该窗口的服务时间更新为该对象到达时间加上processing time;
还有一点注意最好在全程都全部转化为秒,好计算。
下面是我的的丑代码
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main(){
map<int,int> customers;
int n,k,hh,mm,ss,process;
double wait=0.0;
cin>>n>>k;
vector<int> windows(k,8*60*60);
for(int i=0;i<n;i++){
scanf("%d:%d:%d %d",&hh,&mm,&ss,&process);
if(hh*60*60+mm*60+ss<=15*60*60)
customers[hh*3600+mm*60+ss]=process*60;
}
for(auto it=customers.begin();it!=customers.end();it++){
int win_no=min_element(windows.begin(),windows.end())-windows.begin();
if(windows[win_no]>=it->first){
wait+=windows[win_no]-it->first;
windows[win_no]+=it->second;
}else if(windows[win_no<it->first])
windows[win_no]=it->first+it->second;
}
if(customers.size()==0) cout<<0.0;
else printf("%.1f",wait/(60*customers.size()));
return 0;
}