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 (≤104) - 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 HHis 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
思路:
时间转化为秒便于计算,只需要将顾客按到来时间排队,然后逐个判断即可。窗口可以使用优先队列,每次返回时间最早的窗口,用于接待顾客。
注意:
- 题目要求平均等待时间,不包括办理业务的时间
- 优先队列的写法需要注意
代码:(C++)
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct Per
{
int come_time;
int mins;
};
bool cmp(Per a, Per b)
{
return a.come_time < b.come_time;
}
vector<Per> pq;
priority_queue<int,vector<int>, greater<int> > windows;
int main()
{
int N,K;
cin>>N>>K;
int sum = 0;
for(int i=0; i<N; i++)
{
Per pe;
int a,b,c;
scanf("%d:%d:%d %d",&a,&b,&c,&pe.mins);
pe.come_time = a*60*60+b*60+c;
if(pe.come_time > 17*60*60)
continue; //17:00:00后来的,不记录
pq.push_back(pe);
}
N = pq.size(); //被服务的人数
sort(pq.begin(),pq.end(),cmp);
for(int i=0; i<pq.size(); i++)
{
if(windows.size()<K)
{
if(pq[i].come_time < 8*60*60)
{
windows.push(8*60*60+pq[i].mins*60); //窗口下次可服务时间
sum += 8*60*60-pq[i].come_time; //开门前的等待
}
else
windows.push(pq[i].come_time+pq[i].mins*60);
}
else
{
int start_time = windows.top();
windows.pop();
if(start_time < pq[i].come_time) //可以直接开始,不需要等待
windows.push(pq[i].come_time + pq[i].mins * 60);
else
{
windows.push(start_time + pq[i].mins * 60);
sum += start_time-pq[i].come_time;
}
}
}
printf("%.1lf",sum/(60.0*N));
return 0;
}