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
分析:模拟排队问题。注意(1)利用优先队列使插入的元素有序排列;(2)注意分析客户是否需要等待的情况;
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
struct node{
int h,m,s,time;
node(){}
node(int hour, int min, int ss, int t){
h = hour;
m = min;
s = ss;
time = t;
}
bool operator < (const node& a) const{
if(h > a.h)
return true;
else if(h==a.h && m>a.m)
return true;
else if(h==a.h && m==a.m && s>a.s)
return true;
else
return false;
}
};
struct win{
int h, m, s;
win(){}
win(int hh, int mm, int ss){
h = hh;
m = mm;
s = ss;
}
bool operator<(const win& a)const{
if(h>a.h)
return true;
else if(h==a.h&&m>a.m)
return true;
else if(h==a.h&&m==a.m&&s>a.s)
return true;
else
return false;
}
};
priority_queue<node> vec;
priority_queue<win> wins;
int main(int argc, char** argv) {
int n, k, i, h, m, s, time, num;
double wait = 0;
char ch;
cin>>n>>k;
for(i=0; i<n; i++){
cin>>h>>ch>>m>>ch>>s>>time;
if(h>17 || (h==17&&m) || (h==17 && m==0 && s!=0) ){
//17:00以后到达的不受理
continue;
}
vec.push(node(h,m,s,time));
}
num = vec.size();
//窗口初始化为 08:00:00
for(i=0; i<k; i++){
wins.push(win(8,0,0));
}
node cust;
win w;
while(!vec.empty()){
cust = vec.top();
vec.pop();
w = wins.top();
wins.pop();
if(cust.h<w.h||(cust.h==w.h&&cust.m<w.m)||(cust.h==w.h&&cust.m==w.m&&cust.s<w.s)) {
//需要等待的情况下
wait += (w.h-cust.h)*60.0+(w.m-cust.m)+(w.s-cust.s)/60.0;
w.m += cust.time;
w.h += w.m/60;
w.m %= 60;
}else{
//不需要等待的情况下
w.s = cust.s;
w.m = cust.m + cust.time;
w.h = cust.h + w.m/60;
w.m %= 60;
}
wins.push(w);
}
printf("%.1lf\n",wait/num);
return 0;
}
优先队列的另一种用法:
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct win{
win(){
h=8; m=0; s=0;
size = 0;
}
int h,m,s;
int size;
bool operator > (const win & b)const{
if(size != b.size){
return size>b.size;
}else{
return h*3600+m*60+s > b.h*3600+b.m*60+b.s;
}
}
};
bool cmpWin(win a, win b){
if(a.size != b.size){
return a.size<b.size;
}else{
return a.h*3600+a.m*60+a.s < b.h*3600+b.m*60+b.s;
}
}
struct cus{
cus(int _h, int _m, int _s, int _pro){
h = _h; m = _m; s = _s;
pro = _pro;
}
int h,m,s,pro;
int wait;
};
bool cmp(cus a, cus b){
return a.h*3600+a.m*60+a.s < b.h*3600+b.m*60+b.s;
}
int N,K;
priority_queue<win,vector<win>, greater<win> > wins;
vector<cus> custom;
void countWaitTime(cus &c, win &w){
if(w.size==0){
w.size++;
}
long timeC, timeW;
timeC = c.h*3600+c.m*60+c.s;
timeW = w.h*3600+w.m*60+w.s;
if(timeC<timeW){
c.wait = timeW-timeC;
int t = w.m;
w.m = (t + c.pro)%60;
w.h = (t + c.pro)/60 + w.h;
}else{
c.wait = 0;
w.s = c.s;
w.m = (c.pro+c.m)%60;
w.h = (c.pro+c.m)/60 + c.h;
}
}
int main(int argc, char** argv) {
scanf("%d%d",&N,&K);
int i,h,m,s,pro;
for(i=0; i<K; i++){
win w;
wins.push(w);
}
for(i=0; i<N; i++){
scanf("%d:%d:%d %d",&h,&m,&s,&pro);
if((h*3600+m*60+s)>(17*3600))
continue;
if(pro>60){
pro = 60;
}
custom.push_back( cus(h,m,s,pro) );
}
sort(custom.begin(), custom.end(), cmp);
double total = 0;
for(i=0; i<custom.size(); i++){
win w = wins.top();
wins.pop();
countWaitTime(custom[i], w);
total += custom[i].wait;
wins.push(w);
}
total /= 60.0*(custom.size());
printf("%.1lf\n",total);
return 0;
}