PAT甲级 1017 Queueing at Bank

PAT甲级 1017 Queueing at Bank

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)N (≤10​^4)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:SSHH:MM:SSHH: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类似但是注意我题干标红的地方,只有到达银行时间晚于五点的才不计入平均等待时间,如果某人五点前进入银行,但是轮到他已经迟于五点了,他还是要计入平均等待时间,我还是用vector模拟队列,AC代码如下:

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

struct people{
    int starttime,time,endtime,wait,arrivetime;
}p[10005];

bool cmp(people a,people b){
    return a.arrivetime<b.arrivetime;
}

int main(){
    vector<people>l;
    int n,k,hour,minute,second,time;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        scanf("%d:%d:%d %d",&hour,&minute,&second,&time);
        p[i].time=60*time;
        p[i].arrivetime=hour*3600+minute*60+second;//将所有时间化为秒,方便计算
    }
    sort(p,p+n,cmp);
    int ans=0,cnt=0;
    for(int i=0;i<n;i++){
        if(i<k){
            cnt++;
            if(p[i].arrivetime<8*3600){
                ans+=8*3600-p[i].arrivetime;
                p[i].starttime=8*3600;
            }
            else p[i].starttime=p[i].arrivetime;
            p[i].endtime=p[i].starttime+p[i].time;
            l.push_back(p[i]);
        }
        else{
            int minendtime=1e9,minid=0;
            for(int i=0;i<k;i++){
                if(l[i].endtime<minendtime){
                    minendtime=l[i].endtime;
                    minid=i;
                }
            }
            if(p[i].arrivetime>=17*3600) continue;
            else{
                cnt++;
                if(minendtime>=p[i].arrivetime) {ans+=minendtime-p[i].arrivetime;}
                p[i].starttime=max(minendtime,p[i].arrivetime);
                p[i].endtime=p[i].starttime+p[i].time;
                l.erase(l.begin()+minid); l.push_back(p[i]);
            }
        }
    }
    printf("%.1f",double(ans)/double(60*cnt));
}
PAT甲级 1017 Queueing at BankPAT甲级 1017 Queueing at Bank 旺 崽 发布了263 篇原创文章 · 获赞 13 · 访问量 2万+ 私信 关注
上一篇:【思维 + 贪心】AcWing 134. 双端队列


下一篇:经济学人单词-01