PAT 1017. Queueing at Bank (25)(优先队列排队)

来源:互联网 发布:怎么把备份的数据恢复 编辑:程序博客网 时间:2024/05/16 10:50

题目

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 * 60){        total = _h * 3600 + _m *60 + _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 = 28800;    int clk_limit = 61201;    int wait_total = 0;    for(int i = 0; i < K; i++){        windows.push(Window(clk_base));    }    int h,m,s,l;    for(int i = 0; i < N; i++){        scanf("%d:%d:%d%d",&h,&m,&s,&l);        persons.push(Person(h,m,s,l));    }    int cnt = 0;    Person p = Person(0,0,0,0);    Window w = Window(0);    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 / 60.0);    return 0;}

优先队列测试

#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 = 0; i < 10; ++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 = 0; i < 10; ++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 0;}
0 0
原创粉丝点击