您的位置:首页 > 产品设计 > UI/UE

1017. Queueing at Bank (25)

2016-05-24 15:09 525 查看
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


1.用到优先队列priority_queue,它有三个模板参数:元素类型,容器类型,比较算子(默认less,即是利用operator<形成降序排序)

当元素类型是结构体,可用运算符重载来改变比较算子。如本题目,将时间小的放在队首(始终都是),便于计算,省了很多事。

使用时不用自己可以排序,插入元素会自动排好。

2.优先队列的操作不同与普通队列,empty() top()
pop() push()

CODE

代码借鉴别人的,但是学到了很多东西

#include<iostream>
#include<cstdio>
#include<queue>
#include<fstream>
#include<iomanip>
using namespace std;
struct Customer{
int hh;
int mm;
int ss;
int process;
int operator<(const Customer& c)const{
if(hh>c.hh){
return 1;
}else if(hh==c.hh&&mm>c.mm){
return 1;
}else if(hh==c.hh&&mm==c.mm&&ss>c.ss){
return 1;
}else{
return 0;
}
}
};
struct Window{
int hh;
int mm;
int ss;
int operator<(const Window& w)const{
if(hh>w.hh){
return 1;
}else if(hh==w.hh&&mm>w.mm){
return 1;
}else if(hh==w.hh&&mm==w.mm&&ss>w.ss){
return 1;
}else{
return 0;
}
}
};

int main(){
//freopen("input.txt","r",stdin);
int N,K;
cin>>N>>K;
priority_queue<Customer> cus;
priority_queue<Window> win;
Customer c;
Window w;
for(int i=0;i<K;i++){
w.ss=0;
w.mm=0;
w.hh=8;
win.push(w);
}
for(int i=0;i<N;i++){
scanf("%d:%d:%d %d",&c.hh,&c.mm,&c.ss,&c.process);
cus.push(c);
}
int count=0;
double total=0;
while(!cus.empty()){
c=cus.top();
cus.pop();
if(c.hh>17||(c.hh==17&&c.mm)||(c.hh==17&&c.mm==0&&c.ss)){
break;
}
count++;
w=win.top();
win.pop();
if(c.hh<w.hh||(c.hh==w.hh&&c.mm<w.mm)||(c.hh==w.hh&&c.mm==w.mm&&c.ss<w.ss)){
total+=(w.hh-c.hh)*60.0+(w.mm-c.mm)+(w.ss-c.ss)/60.0;
w.mm+=c.process;
w.hh+=w.mm/60;
w.mm%=60;
}else{
w.ss=c.ss;
w.mm=c.mm+c.process;
w.hh=c.hh+w.mm/60;
w.mm%=60;
}
win.push(w);
}
printf("%0.1lf",total/count);
//fclose(stdin);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: