PAT1017 Queueing at Bank (25 分)

3 篇文章 0 订阅
2 篇文章 0 订阅

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968

题目描述:

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

解题思路:

          本题目主要考察优先队列、排序的思路。

         1、我们按照消费者到达时间排序(越早到达的优先进行事务处理)。

         2、对前k个用户到达1-k窗口,如果用户提早到达,那么需要添加等待时间(营业开始时间- 用户到达时间),该窗口最早结束时间 ear:营业开始时间+用户处理时间。否则 不需要添加等待时间,窗口最早结束时间 = 用户到达时间+用户处理事务时间。

         3、维护一个优先队列,对于每个窗口,每次取出最早完成时间的窗口 t。如果 t >关门时间,则直接退出。如果对应得no 属于 k - n 中取出 ps[no]的处理时间+最早完成时间,在关门前结束的用户处理,如果用户到达时间arr > ear 则不添加总的等待时间,否则添加 ear - arr 的等待时间,最新窗口信息更新,添加到队列中。

         4、输出结果。

23分代码

#include <iostream>
#include<bits/stdc++.h>
#define rep(i,s,e) for(int i = s;i<e;i++)
using namespace std;
//https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968
//时间单位为秒
const int WORKTIME = 3600*17;
const int STARTTIME = 3600*8;
struct P{
    int arrT;
    int pT;
   // int pwt;
};
struct W{
    int earT; //最早处理完时间
    friend bool operator < (W a,W b){
        return a.earT >b.earT;
    }
};
int n,k;
int no,swt,dpn;//当亲序号,总时间,共处理的人数
P *ps;
W *ws;
bool cmp(P p1,P p2){
    return p1.arrT<p2.arrT;
}
void getRe(){
    if(n<k){
        cout<<0;
        return ;
    }
    swt = 0;
    sort(ps,ps+n,cmp);
    priority_queue<W> wq;
    rep(i,0,k){
        W w2;
        if(ps[i].arrT<STARTTIME){
          w2.earT = STARTTIME+ps[i].pT;
          swt+= STARTTIME- ps[i].arrT;
        }
        else w2.earT = ps[i].arrT+ps[i].pT;
        wq.push(w2);
    }
    W w = wq.top(); // 取出头
    wq.pop();
    //进行计算
    for(no=k;no<n;no++){
        if(w.earT>WORKTIME||ps[no].arrT>WORKTIME){
            break;
        }
        if(w.earT+ps[no].pT>WORKTIME){
            continue;
        }
        //等待的情况
        if(w.earT>=ps[no].arrT){
            swt += w.earT - ps[no].arrT;
            dpn++;
            w.earT = w.earT+ps[no].pT;
            wq.push(w);
        } // 用户等待了
        else{
            dpn++;
            w.earT = ps[no].arrT+ps[no].pT;
            wq.push(w);
        }
       // cout<<ps[no].arrT<<endl;
        w = wq.top();
        wq.pop();
    }
    dpn+=k;
    printf("%.1f",(1.0)*swt/dpn/60);
}
void init(){
    cin>>n>>k;
    ps = new P[n];
    rep(i,0,n){
        int h,m,s,pt;
        scanf("%d:%d:%d %d",&h,&m,&s,&pt);
        if(pt>60){
            pt = 3600;
        }else{
            pt *=60;
        }
        ps[i].arrT = h*3600+m*60+s;
        ps[i].pT = pt;
    }
}
int main()
{
    init();
    getRe();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值