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.
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.
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.
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
8.2
自己写的代码,基本思路都是没错的,但是有个问题是在求三个窗口最快的结束时间我用的是循环找到最快结束的窗口,但是不知道为什么就出现错误了,还是得多写写题目,提高自己得代码水平!
#include
#include
#include
using namespace std;
typedef pair PII;
vector v;//p表示当前机器使用者的开始时间和处理时间,s表示客户的信息
int main(){
int n,k,num=0,sum=0;
cin >> n >> k;
int q[k]={0},w[k]={0};//分别表示当前用户的开始时间和处理业务所需要的时间
int t,h,m,s,r,T;
for(int i=0;i=17*60*60) continue;
v.push_back({t,T});
num++;
}
sort(v.begin(),v.end());
int j;
for(int i=0;iq[j]+w[j]){
minn=q[j]+w[j];//找到最快结束的机子
}
}
q[j]=a,w[j]=b;
cout << minn << endl;
if(minn<=a) continue;//结束时间<=到达时间,无需等待,直接用
sum+=minn-a;//前面有人再用着,等待时间
}
cout << sum << endl;
//printf("%.1lf\n",((double)sum)/60/num);
return 0;
}
看看大佬的代码:
#include
#include
#include
using namespace std;
const int N=10010;
struct person{
int come,time;
}p[N];
bool cmp(person a,person b){
return a.come61200) continue;
p[++cnt].time=tt*60;
p[cnt].come=sum;
}
sort(p+1,p+1+cnt,cmp);
priority_queue,greater> q;
for(int i=1;i<=k;i++) q.push(28800);
for(int i=1;i<=cnt;i++){
if(q.top()<=p[i].come){
q.push(p[i].come+p[i].time);
q.pop();
}
else{
total+=q.top()-p[i].come;
q.push(q.top()+p[i].time);
q.pop();
}
}
(!cnt)?printf("0.0\n"):printf("%.1lf",((double)total/60.0)/(double)cnt);
return 0;
}
好好学习,天天向上!
我要考研!