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

思路:

和1014不一样,这里如果按照分钟一点点累加模拟计时的话,到达时间精确到秒的顾客就会报错,所以把所有时间先转化为秒钟。
还有一个坑点,17:00之后的顾客不接受服务,但是到达时间在17:00及之前客户,就算17:00之后还没轮到,银行也会服务完。所以时间是没有限制的,要所有17:00及之前到达的客户服务完才结束循环。

源码:

#include<bits/stdc++.h>

#define Max 10000
using namespace std;
int Rank[100];
int last[100] = {0};
double wait[Max] = {0};
typedef struct person {
    int h, m, s;
    double past_m;
    int cost;
} PP;
PP people[Max];
int t = 0;

bool compare(PP a, PP b) {
    return a.past_m < b.past_m;
}

int main() {
    int n, k;
    cin >> n >> k;
    int N = 0;
    for (int i = 0; i < n; i++) {
        string t;
        cin >> t >> people[N].cost;
        int h, m, s;
        h = atoi(t.substr(0, 2).c_str());
        m = atoi(t.substr(3, 2).c_str());
        s = atoi(t.substr(6, 2).c_str());
        people[N].h = h;
        people[N].m = m;
        people[N].s = s;
        people[N].past_m = (h - 8) * 60 * 60 + m * 60 + s;
        if (strcmp(t.c_str(), "17:00:00") <= 0)
            N++;
    }
    if (!N) {
        cout << 0.0;
        return 0;
    } else {
        fill(Rank, Rank + 100, -1);
        sort(people, people + N, compare);

        int cnt = 0;
        for (int i = 0; i < N; i++) {
            if (people[i].h < 8 && i < k) {
                Rank[i] = i;
                cnt = i + 1;
                wait[i] = (8 - people[i].h) * 60 - people[i].m - people[i].s / 60.0;
            }
        }

        while (cnt < N) {
            for (int i = 0; i < k; i++) {
                if (Rank[i] == -1) {
                    if (people[cnt].past_m <= t) {
                        wait[cnt] = (t - people[cnt].past_m) / 60.0;
                        Rank[i] = cnt++;
                        last[i] = t;
                    }
                } else if (people[Rank[i]].cost * 60 == t - last[i]) {
                    Rank[i] = -1;
                    if (people[cnt].past_m <= t) {
                        wait[cnt] = (t - people[cnt].past_m) / 60.0;
                        Rank[i] = cnt++;
                        last[i] = t;
                    }
                }
            }
            t++;
        }
        printf("%.1f", accumulate(wait, wait + N, 0.0) / N);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值