PTA A1017

A1017 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 (≤104) - 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

題目分析

這道題其實思路不是很難的,畢竟先做過了一個30分的類似題,思路是很相似的,需要注意的是如果到達時間不超過17:00,而完成的時間超過17:00這種情況是要計算在內的,只是我有一個疑問如果到了17:00,還沒排到隊的人是否應該繼續等待?按常識是不會的,可是這道題目按常識來是不對的。。。我當時加了一個判斷如果一個窗口的endtime大於了17:00,那麼後面的客戶等待時間應該是到17:00就結束了,繼續等下去顯然是有點傻的,因爲人家已經不服務了啊,顯然對於一道模擬題,我覺得這裏設計的不太好啊。
這題似乎還有另一個小bug,大家可以嘗試一下,如果大家忘記題目給的It is assumed that no window can be occupied by a single customer for more than 1 hour.條件,即不判斷每一個人的處理過程是否超過了一個小時,這道題目依然能AC,我一開始沒注意這個條件,最後一個測試點過不了以爲是這裏的問題,可是修改後依然過不了。最後我把之前代碼都給刪除掉用秒作爲基本單位,而不是分(我一開始用的是分作爲比較單位,比較麻煩,我懷疑最後一個點A不了和這個有點關係),A掉了,我特意把在前面判斷一個每個人停留時間是否超過1個小時的代碼刪除,發現依然A掉了。。。這顯然是測試系統忘記了這個條件,感興趣的大家可以嘗試一下。
基於上面兩個原因,雖然我的思路一開就想好了,但A題的過程很不順利,唉~

具體代碼

    #include<stdio.h>
    #include<stdlib.h>

    int endtime[105] = { 0 };
    struct person
    {
        int sec;
        int pro;
    };

    struct person p[10010];

    int N, M;
    int all_wait = 0;

    int cmp(const void *p, const void *q)
    {
        return ((struct person*)p)->sec - ((struct person*)q)->sec;
    }

    int main(void)
    {
    scanf("%d %d", &N, &M);
    for (int i = 0; i < N; i++)
    {
        int h, m, s, t;
        scanf("%d:%d:%d %d", &h, &m, &s, &t);
        if (t * 60 > 3600)
            p[i].pro = 60 * 60;
        else
            p[i].pro = t * 60;
        p[i].sec = h * 60 * 60 + m * 60 + s;
    }
    for (int i = 0; i < M; i++)
    {
        endtime[i] = 8 * 60 * 60;
    }
    int i;
    qsort(p, N, sizeof(struct person), cmp);
    for (i = 0; (i < N) && (p[i].sec <= 17 * 60 * 60); i++)
    {
        int earliest = 0;
        for (int j = 0; j < M; j++)
            if (endtime[j] < endtime[earliest])
                earliest = j;
        if (p[i].sec < endtime[earliest])
        {            
            all_wait += endtime[earliest] - p[i].sec;
            endtime[earliest] += p[i].pro;
        }
        else
            endtime[earliest] = p[i].sec + p[i].pro;
    }
    if (i)
        printf("%.1f", all_wait / 60.0 / i);
    else
        printf("0.0");
    system("pause");
    }

參考博客

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