題目
1017. Queueing at Bank (25)
時間限制
400 ms
記憶體限制
65536 kB
代碼長度限制
16000 B
判題程式
Standard
作者
CHEN, Yue
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 10
Sample Output:
8.2
解題思路
- 1.用優先隊列,取出優先人(到的最早),取出優先視窗(視窗完工的時間最早),如果這個人到的時間比視窗完工的時間晚,那麼就需要等了,否則則不要,另外,如果人到的時間超過17:00了就不要再計算了。
AC代碼(參考《最美的時光》)
#include<iostream>
#include<iomanip>
#include<queue>
#include<stdio.h>
using namespace std;
struct Person{
int hour;
int minute;
int second;
int last;
int total;
Person(int _h, int _m, int _s, int _l) : hour(_h), minute(_m), second(_s), last(_l * ){
total = _h * + _m * + _s;
}
bool operator < (const Person& p) const {
if( total > p.total ){
return true;
}else{
return false;
}
}
};
struct Window{
int total;
Window(int _t):total(_t){}
bool operator < (const Window& w) const{
if( total > w.total ){
return true;
}else{
return false;
}
}
};
int main(){
priority_queue<Person> persons;
priority_queue<Window> windows;
int N,K;
cin >> N >> K;
int clk_base = ;
int clk_limit = ;
int wait_total = ;
for(int i = ; i < K; i++){
windows.push(Window(clk_base));
}
int h,m,s,l;
for(int i = ; i < N; i++){
scanf("%d:%d:%d%d",&h,&m,&s,&l);
persons.push(Person(h,m,s,l));
}
int cnt = ;
Person p = Person(,,,);
Window w = Window();
while(!persons.empty()){
p = persons.top();
persons.pop();
if(p.total >= clk_limit) break;
cnt++;
w = windows.top();
windows.pop();
if(p.total < w.total){
wait_total += w.total - p.total;
w.total += p.last;
}else{
w.total = p.total + p.last;
}
windows.push(w);
}
printf("%0.1f",wait_total / cnt / );
return ;
}
優先隊列測試
#include<queue>
#include<iostream>
using namespace std;
struct person{
int n;
person(int _n):n(_n){}
//第二個const??!!!
bool operator < (const person & a) const {
return n>a.n;
}
};
int main(int argc, char *argv[])
{
cout << "預設:" << endl;
priority_queue<int> p;
for (int i = ; i < ; ++i) {
p.push(i);
}
//輸出是 9 8 7 6 5 4 3 2 1 0,即預設是由大到小排列
while (!p.empty()) {
int a = p.top();
cout << a << " ";
p.pop();
}
cout << endl;
//重置為從小到大排列
priority_queue<person> p2;
for (int i = ; i < ; ++i) {
p2.push(person(i));
}
//輸出是 0 1 2 3 4 5 6 7 8 9
while (!p2.empty()) {
person a = p2.top();
cout << a.n << " ";
p2.pop();
}
return ;
}