PAT (Advanced Level) Practice 1017 Queueing at Bank (25 point(s))

题目:PAT A 1017

题目大意

给出N个客户的到达时间,银行有K个窗口,银行在8:00上班,在17:00后到达的客户不服务,也不计算计算到平均时间。请问平均等待时间

解题思路

  • 根据客户达到的时间进行排序。使用window数组模拟银行的窗口。每次分配给你用户的窗口的是时间最小的。
#include<bits/stdc++.h>
using namespace std;
struct Customer{
    int h,m,s,p;
    int time;
};
int n,k;
int startime=8*60*60;
int endtime=17*60*60;
int window[101];
bool cmp(const Customer &a,const Customer &b){
    return a.time<b.time;
}
vector<Customer>customer;
int main(){
    scanf("%d%d",&n,&k);
    customer.resize(n);
    for(int i=0;i<101;i++) window[i]=startime;
    for(int i=0;i<n;++i){
        scanf("%d:%d:%d %d",&customer[i].h,&customer[i].m,&customer[i].s,&customer[i].p);
        customer[i].time=customer[i].h*60*60+customer[i].m*60+customer[i].s;
    }
    sort(customer.begin(),customer.end(),cmp);   
    double total=0;
    int cnt=0;
    for(int i=0;i<n;++i){
        int push=0;
        if(customer[i].time>endtime) break;
        for(int j=0;j<k;++j){
            if(window[j]<window[push]) push=j;
        }
        ++cnt;
        if(customer[i].time>=window[push]){//窗口空闲,不需要等待
            window[push]=customer[i].time+customer[i].p*60;
        }else{//窗口无空闲,需要等待
            total+=window[push]-customer[i].time;
            window[push]=window[push]+customer[i].p*60;             
        }
    }
    printf("%.1f\n",total/60.0/cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值