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 (≤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 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
按时间排序之后,每次选出窗口中结束时间最小的,让当前时间更新到该时间,然后让下一个来到窗口,重复该过程。
注意一下规则方面的细节。
#include<bits/stdc++.h>
using namespace std;
struct node{
int sc;
int cx;
}p[11111];
bool cp(node x,node y)
{
return x.sc<y.sc;
}
int js(int h1,int m1,int s1,int h2,int m2,int s2)
{
int ans=0;
int f=1;
if(h1>h2)
{
swap(h1,h2);
swap(m1,m2);
swap(s1,s2);
f=-1;
}
if(h1!=h2)
{
ans+=(h2-h1-1)*60*60;
ans+=60-s1;
ans+=(60-m1-1)*60;
ans+=s2;
ans+=m2*60;
}
else{
if(m1!=m2)
{
ans+=60-s1;
ans+=s2;
ans+=(m2-m1-1)*60;
}
else ans+=s2-s1;
}
return ans*f;
}
int main()
{
int n,k;
cin>>n>>k;
char jl[22];
int shi,fen,miao;
for(int i=0;i<n;i++)
{
cin>>jl;
shi=(jl[0]-'0')*10+jl[1]-'0';
fen=(jl[3]-'0')*10+jl[4]-'0';
miao=(jl[6]-'0')*10+jl[7]-'0';
cin>>p[i].cx;
if(p[i].cx>60)
p[i].cx=60;
p[i].sc=js(8,0,0,shi,fen,miao);
}
sort(p,p+n,cp);
double ans=0;
int zz=n;
int ck[111];
memset(ck,0,sizeof(ck));
for(int i=0;i<n;i++)
{
if(p[i].sc<0)
{
ans+=abs(p[i].sc);
p[i].sc=0;
}
if(p[i].sc>32400)
{
zz=i;
break;
}
}
if(zz==0||n<=k)
{
cout<<"0.0"<<endl;
return 0;
}
for(int i=0;i<zz;i++)
{
int id;
int minn=999999999;
for(int j=0;j<k;j++)
{
if(ck[j]<minn)
{
minn=ck[j];
id=j;
}
}
//cout<<ck[id]<<endl;
if(ck[id]<=p[i].sc)
{
ck[id]=p[i].sc+p[i].cx*60;
}
else{
ans+=ck[id]-p[i].sc;
ck[id]+=p[i].cx*60;
}
}
if(ans==0)
{
cout<<"0.0"<<endl;
return 0;
}
ans=ans/60.0/(zz*1.0);
printf("%.1lf\n",ans);
return 0;
}