天天看點

PAT甲級-1017 Queueing at Bank (25分)

點選連結PAT甲級-AC全解彙總

題目:

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
           

題意:

銀行八點開門,下午五點關門,關門之前所有來的人都會處理!!那怕都已經到第二天了,照樣會處理完!最後一個case的考點!

輸入的是到達時間和需要處理的時間,計算所有人等待的平均值。

沒有考察0人的情況,是以輸出的時候沒必要判斷cnt是否為0,不過最好還是寫一下養成習慣。

思路是把所有時間用秒表示,這樣友善多了。本來還以為我真是個機靈鬼,寫完查了下其他大佬也都是這樣處理的…

不過我有個不了解的地方是為什麼輸入的時候如果發現這個人超過五點到達直接不計入統計的話,case0 1會錯誤,這是什麼原因呢?好在一開始我是在統計的時候做的判斷。

我的代碼:

#include<bits/stdc++.h>
using namespace std;

class Person{
public:
    Person(int a,int b):arrive_(a),time_need_(b){}
    int arrive_;
    int time_need_;
};

bool cmp(Person a,Person b)
{
    return a.arrive_<b.arrive_;
}

int main()
{
    int N,W,sum_wait=0,sum_cnt=0;
    cin>>N>>W;
    int widows[W]={0};
    vector<Person>persons;
    for(int i=0;i<W;i++)widows[i]=8*3600;
    for(int i=0;i<N;i++)
    {
        int hh,mm,ss,time_need;
        scanf("%d:%d:%d %d",&hh,&mm,&ss,&time_need);
        int arrive=hh*3600+mm*60+ss;
//        if(arrive>17*3600)continue;//這裡判斷的話case0 1會報錯
        if(time_need>60)time_need=60;
        Person t(arrive,time_need*60);
        persons.push_back(t);
    }
    sort(persons.begin(),persons.end(),cmp);
    for(int i=0;i<N;i++)
    {
        //找視窗時間最小的
        int min_time=240*3600,index=0;
        for(int i=0;i<W;i++)
        {
            if(min_time>widows[i])
            {
                min_time=widows[i];
                index=i;
            }
        }
        if(persons[i].arrive_>17*3600)continue;
        if(persons[i].arrive_<min_time)//需要等
        {
            sum_wait+=min_time-persons[i].arrive_;
            widows[index]+=persons[i].time_need_;
        }
        else
        {
            widows[index]=persons[i].arrive_+persons[i].time_need_;
        }
        sum_cnt++;
    }

    printf("%.1f\n",1.0*sum_wait/60/sum_cnt);//case沒有考察對0人的情況
    return 0;
}

           

繼續閱讀