PTA 1017 Queueing at Bank

题目描述

在这里插入图片描述

分析:复杂模拟,模拟银行等待,使用优先队列进行模拟

#include<vector>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cLIMITS>
using namespace std;

struct customer{
    int arrive;
    int process;
}cs;
bool cmp(const customer& c1,const customer& c2){
    return c1.arrive<c2.arrive;
}
int main(){
    int open=8*60*60;
    int close=17*60*60;
    int n,k;
    scanf("%d%d",&n,&k);
    priority_queue<int,vector<int>,greater<int>> window;
    vector<customer> cus;
    double wait=0;
    while(n--){
        int h,m,s;
        scanf("%d:%d:%d %d",&h,&m,&s,&cs.process);
        cs.arrive=(h*60+m)*60+s;
        cs.process*=60;
        if(cs.arrive<=close){
            cus.push_back(cs);
        }
    }
    sort(cus.begin(),cus.end(),cmp);
    for(int i=0;i<cus.size();i++){
        if(cus[i].arrive<open){
            wait+=open-cus[i].arrive;
            cus[i].arrive=open;
        }
        if(window.size()==0){
            window.push(cus[i].arrive+cus[i].process);
        }
        else{
            int time=window.top();
            while(time<=cus[i].arrive){
                window.pop();
                if(!window.empty())
                time=window.top();
                else{
                    time=INT_MAX;
                }
            }
            if(window.size()<k){
                window.push(cus[i].arrive+cus[i].process);
            }
            else{
                time=window.top();
                window.pop();
                wait+=time-cus[i].arrive;
                window.push(time+cus[i].process);
            }
        }
    }
    wait/=cus.size()*60.0;
    printf("%.1f",wait);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值