【剑指紫金港】1017 Queueing at Bank 排队模拟

A 1017 Queueing at Bank

题目链接

Problem Description

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

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 HHis in the range [00, 23], MMand SSare 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

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

题目大意

模拟排队问题,k个窗口,n个人,可开始任务时间为8:00——17:00,每个任务不超过1小时,问所有人的平均排队时间是多少(开始任务时间在17:00之后的人数不算)

解题思路

n个人,记录每个人的到达时刻和办事时间;k个窗口,记录当前窗口的结束时刻;遍历k个窗口若干次,每次安排一个人到结束时刻最早的窗口,直到n个人安排完为止。

AC代码

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f

const int start = 8*3600;
const int last = 17*3600;
const int maxn = 105;

struct node{
    int t;
    int ti;
};

int win[maxn],n,k,h,m,s,p;

bool check(int x){
    if(x<=last){
        return true;
    }
    return false;
}

bool cmp(const node &a,const node &b){
    return a.t<b.t;
}

int main(){
    scanf("%d%d",&n,&k);
    vector<node> man(n);
    fill(win,win+k,INF);
    int num=n;
    for(int i=0;i<n;i++){
        scanf("%2d:%2d:%2d %d",&h,&m,&s,&p);
        man[i].t=3600*h+60*m+s;
        man[i].ti=p*60;
        if(!check(man[i].t)){
            num--;
        }
    }
    int ind=0,ans=0,i;
    sort(man.begin(),man.end(),cmp);
    for(i=0;i<n;i++){
        if(check(man[i].t) && ind<k){
            ans+=max(0,start-man[i].t); //等待时间
            win[ind++]=max(start,man[i].t)+min(3600,man[i].ti); //结束时间
        }else{
            break;
        }
    }
    if(i==k){
        while(i<n && check(man[i].t)){
            int minwin,mint=INF;
            for(int j=0;j<k;j++){
                if(win[j]<mint){
                    mint=win[j];
                    minwin=j;
                }
            }
            ans+=max(0,win[minwin]-man[i].t);
            win[minwin]=max(win[minwin],man[i].t)+min(3600,man[i].ti);
            i++;
        }
    }
    if(num==0){
        ans=0;
    }
    printf("%.1f",(double)1.0*ans/60/num);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值