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 (<=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


优先队列

代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
struct times;
bool comptime(times,times);
struct times
{
    int hh,mm,ss;
}t,t1,t2;
struct people
{
    int process;
    times t;
    people(){}
    people(times a,int p)
    {
        t = a;
        process = p;
    }
    friend bool operator < (people a,people b)
    {
        return comptime(b.t,a.t);//如果a比b大 就调换位置
    }
}temp,s[10000];
bool comptime(times a,times b)
{
    if(a.hh == b.hh)
    {
        if(a.mm == b.mm)return a.ss < b.ss;
        return a.mm < b.mm;
    }
    return a.hh < b.hh;
}
times update(times a,int process)
{
    process += a.mm;
    a.mm = process % 60;
    a.hh += process / 60;
    return a;
}
int counttime(times a,times b)
{
    return a.hh * 3600 + a.mm * 60 + a.ss - b.hh * 3600 - b.mm * 60 - b.ss;
}
bool cmp(people a,people b)
{
    return comptime(a.t,b.t);
}
int main()
{
    int sum = 0;
    priority_queue<people> q;
    int n,k;
    t1.hh = 8;t1.mm = t1.ss = 0;
    t2.hh = 17;t2.mm = t2.ss = 0;
    cin>>n>>k;
    for(int i = 0;i < n;i ++)
    {
        cin>>s[i].t.hh;
        cin.get();
        cin>>s[i].t.mm;
        cin.get();
        cin>>s[i].t.ss>>s[i].process;
        if(comptime(t2,s[i].t))i --,n --;
    }
    sort(s,s+n,cmp);
//    for(int i = 0;i < n;i ++)
//    {
//        cout<<s[i].t.hh<<':'<<s[i].t.mm<<':'<<s[i].t.ss<<' '<<s[i].process<<endl;
//    }
    for(int i = 0;i < n;i ++)
    {
        if(q.size() < k)
        {
            if(comptime(s[i].t,t1))
            {
                sum += counttime(t1,s[i].t);
                q.push(people(update(t1,s[i].process),s[i].process));
            }
            else q.push(people(update(s[i].t,s[i].process),s[i].process));
        }
        else
        {
            temp = q.top();
            q.pop();
            if(comptime(s[i].t,temp.t))
            {
                sum += counttime(temp.t,s[i].t);
                q.push(people(update(temp.t,s[i].process),s[i].process));
            }
            else q.push(people(update(s[i].t,s[i].process),s[i].process));
        }

//        cout<<q.top().t.hh<<':'<<q.top().t.mm<<':'<<q.top().t.ss<<' '<<q.top().process<<endl;
    }
    printf("%.1f",sum/(60.0*n));
}
原文地址:https://www.cnblogs.com/8023spz/p/7989795.html