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;
}

如果對您有幫助,幫忙點個小拇指唄~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章