天天看点

【PAT】【银行排队繁琐模拟】1017 Queueing at Bank

题目链接: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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10​4​​) - 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
           

 // 繁琐的 银行排队 模拟题

// 解题思路:将每个人的到达时间arr, 处理时间p,离开时间封装在一个结构体

// 对到达时间的先后进行排序,然后进行初始化,先试图填满窗口并把在处理中的客户加入 以离开时间来比较的 优先队列, 初始化其离开时间

// 之后则每处理完一个客户就加入新的一个,并根据新客户的到达时间 和 处理完客户的离开时间得到等待时间以及新客户的离开时间

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct node
{
    long long arr, p, leave;
    // 优先队列的比较方式是离开时间早的在前
    bool operator<(const node &r) const
    {
        return leave > r.leave;
    }
};
// 到达时间的排序
bool cmp(const node &l, const node &r)
{
    return l.arr < r.arr;
}

vector<node> cus;
priority_queue<node> q;
int main()
{
    int n, k, cnt(0);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        char c; int h, m , s;
        node temp;
        // char c字符用来去掉'c'字符
        cin >> h >> c >> m >> c >> s >> temp.p;
        // 转化时间格式为 单位为秒的 整数
        temp.arr = h * 3600 + m * 60 + s;
        // 晚于17:00到来的不再服务
        if(temp.arr > 17 * 3600) continue;

        // 统计会进行处理的人数
        cnt++;
        // 超过一小时的处理时间最多是一小时
        if(temp.p > 60) temp.p  = 60;
        cus.push_back(temp);
    }
    // 按到达时间排序
    sort(cus.begin(), cus.end(), cmp);
    
    int c = 0;//cus[c]
    double ans = 0;
    // 初始化, 先试图填满窗口,以及得到离开时间,之后入队的需要用
    for( ;c < k && c < cnt; c++)
    {
        // 早于 和 晚于开门时间来的离开时间不一样的处理
        if(cus[c].arr < 8 * 3600)
        {
            ans += 8 * 3600 - cus[c].arr;
            cus[c].leave = cus[c].p * 60 + 8 * 3600;
        }
        else
        {
            cus[c].leave = cus[c].p * 60 + cus[c].arr;
        }
        q.push(cus[c]);
    }
    
    // 非空还有人, 则加入窗口处理队列
    while(!q.empty() && c < cnt)
    {
        node temp = q.top(); q.pop();
        // 根据离开客户的离开时间 和 新到窗口的到达时间得到等待时间 和 新的离开时间
        if(cus[c].arr < temp.leave)
        {
            ans += temp.leave - cus[c].arr;
            cus[c].leave = temp.leave + cus[c].p * 60;
        }
        else
        {
            cus[c].leave = cus[c].arr + cus[c].p * 60;
        }
        q.push(cus[c]);
        c++;
    }
    // 如果一个都没有则是0.0不用再除了
    if(ans == 0)
    printf("%.1lf\n", ans);
    else
    printf("%.1lf\n", ans / 60 / cnt);
    return 0;
}