A 1017 Queueing at Bank
Problem Description
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
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 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
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:00——17:00,每个任务不超过1小时,问所有人的平均排队时间是多少(开始任务时间在17:00之后的人数不算)
解题思路:
n个人,记录每个人的到达时刻和办事时间;k个窗口,记录当前窗口的结束时刻;遍历k个窗口若干次,每次安排一个人到结束时刻最早的窗口,直到n个人安排完为止。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int start = 8*3600;
const int last = 17*3600;
const int maxn = 105;
struct node{
int t;
int ti;
};
int win[maxn],n,k,h,m,s,p;
bool check(int x){
if(x<=last){
return true;
}
return false;
}
bool cmp(const node &a,const node &b){
return a.t<b.t;
}
int main(){
scanf("%d%d",&n,&k);
vector<node> man(n);
fill(win,win+k,INF);
int num=n;
for(int i=0;i<n;i++){
scanf("%2d:%2d:%2d %d",&h,&m,&s,&p);
man[i].t=3600*h+60*m+s;
man[i].ti=p*60;
if(!check(man[i].t)){
num--;
}
}
int ind=0,ans=0,i;
sort(man.begin(),man.end(),cmp);
for(i=0;i<n;i++){
if(check(man[i].t) && ind<k){
ans+=max(0,start-man[i].t); //等待时间
win[ind++]=max(start,man[i].t)+min(3600,man[i].ti); //结束时间
}else{
break;
}
}
if(i==k){
while(i<n && check(man[i].t)){
int minwin,mint=INF;
for(int j=0;j<k;j++){
if(win[j]<mint){
mint=win[j];
minwin=j;
}
}
ans+=max(0,win[minwin]-man[i].t);
win[minwin]=max(win[minwin],man[i].t)+min(3600,man[i].ti);
i++;
}
}
if(num==0){
ans=0;
}
printf("%.1f",(double)1.0*ans/60/num);
return 0;
}