PAT(甲) 1017 Queueing at Bank (25)(详解)

1017 Queueing at Bank (25)(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.


  • 输入格式
    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.

  • 输出格式
    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.


题目大意:
这题主要是模拟客户等待的平均时间,按照先来先服务的原则,并且在17:00:01后到来的客户无法进行服务,每个窗口只能有一个人。

解题方法:
这题可以先把时间转换成以秒为单位,然后判断一下是否到来的时间超过61200(即17:00:00),如果超过就舍去,否则就放入数组。然后对数组进行排序,按照先来先服务的原则,去与每个窗口的最早空闲时间进行比对,如果申请服务时,已经空闲,那么窗口的下一个空闲时间为当前时间+服务时间,如果申请服务时,还未空闲,则最早空闲的窗口的下一个空闲时间为原来空闲的时刻+服务时间。


易错点:
1.这题我一开始用的是map,应该map默认是按键升序排列的,但后来发现后面4个测试点无法通过,估计是可能存在大量超时而来的顾客导致的。
2.在对queue排序的时候,一开始第二个参数我写了queue+N,事实上,应该为queue+size,虽然正常人都不会写错,但是我还是说明一下,万一有人就跟我一样粗心呢。。
3.如果没有一个顾客能够在17:00:01前到来,这种情况也需要单独考虑,因为0无法做除数。


程序:

#include <stdio.h>
#include <algorithm>
using namespace std;

struct client
{
    int come, time;
};

bool cmp (client c1, client c2)
{
    return c1.come < c2.come;
}
int main(int argc, char const *argv[])
{
    int N, K, H, M, S, come, time, size = 0;
    scanf("%d %d", &N, &K);
    client queue[N];
    int Win[K];
    fill(Win, Win+K, 28800);    /* 初始化 */
    double sum = 0.0;
    for (int i = 0; i < N; i++)
    {
        scanf("%d:%d:%d %d", &H, &M, &S, &time);
        time = time > 60 ? 60 : time;   /* 超过一小时压缩成一小时 */
        come = H*3600 + M*60 + S;
        if (come > 61200)   /* 超过5点后来的客户不受理 */
            continue;
        queue[size].come = come;
        queue[size].time = time * 60;
        size++;
    }
    sort(queue, queue+size, cmp);   /* 升序排序 */
    for (int i = 0; i < size; i++)
    {
        int earlyPop = Win[0], WinID = 0;
        for (int j = 1; j < K; j++)
            if (Win[j] < earlyPop)
            {   /* 找出最早空闲的窗口笔记录窗口号和时间 */
                WinID = j;
                earlyPop = Win[j];
            }
        if (queue[i].come < earlyPop)   /* 如果来早了 */
        {
            sum += earlyPop - queue[i].come;
            Win[WinID] += queue[i].time;
        }
        else    /* 如果来的时候已有空闲窗口 */
            Win[WinID] = queue[i].come + queue[i].time;
    }
    if (size == 0)
        printf("0.0");
    else
        printf("%.1f", sum / 60.0 / size);
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

猜你喜欢

转载自blog.csdn.net/invokar/article/details/80556798