1017 Queueing at Bank (25分)

题目

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 ( ≤ 1 0 4 ) N (\le10^4) N(104) - the total number of customers, and K ( ≤ 100 ) K (\le100) 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. 上午8:00开始服务,8点之前来的客户需要等到8:00。
  2. 17:0:0来的客户可以得到服务,等于或大于17:0:1的客户不能得到服务,并且在计算平均等待时间时不计入人数。
  3. 每名客户最多只能享受60分钟服务,超过的按60分钟计算。
  4. 客户按先来先服务的顺序排队,当有空闲窗口时则享受服务。

思路

  1. 由于输入数据有秒,所以将所有数据转化为秒来处理。
  2. 对所有数据排序,对到达时间按照从小到大的顺序排序。
  3. 窗口用window[k]表示,初始时为0,window[i]=t,表示窗口it时刻开始空闲,可以服务。
  4. 8:00为起始时间,8*60*60=28800,所有到达时间都减去28800,为负数则表示在8:00之前到达,其绝对值计入等待时间。
  5. 输入数据放在vector中,服务时间超过60的以60代替,到达时间超过17点的不放入vector中。
  6. 遍历vector,每一趟找出可服务时间最短的窗口,可理解为当时间到t时,窗口i空闲,可以服务一名客户t比较窗口可服务时间t与客户到达时间come,当t <= comt时,表示客户一来就能得到服务,无需等待,当t > come时,表示客户需要等待,且等待时间为t - come,计入总的等待时间。

代码

开始这个题有一个测试点没过,所以修改了很久,目前自认为是非常精简的代码了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct node{
    int come, wait;
};

bool cmp(node a, node b){
    return a.come < b.come;
}

int main(){
    int n, k;
    double sum = 0.0;	//总等待时间和
    cin>>n>>k;
    vector<node> cus;
    vector<int> window(k, 0);	//把所有窗口的可服务时间初始化为0
    for(int i=0; i<n; i++){
        int a, b, c, d;
        node tmp;
        scanf("%d:%d:%d %d", &a, &b, &c, &d);
        tmp.come = a*60*60 + b*60 + c - 28800;
        d = d > 60 ? 60:d;	//服务时间超过60按60计算
        tmp.wait = d*60;
        if(tmp.come > 32400)	//到达时间晚于17点,不计入计算
            continue;
        cus.push_back(tmp);
    }
    n = cus.size();		//能享受服务的人数
    sort(cus.begin(), cus.end(), cmp);	//排序
    for(int i=0; i<n; i++){
        int j=0;
        for(int t=1; t<k; t++)		//找出最先空闲的窗口
            if(window[t] < window[j])
                j = t;
        if(window[j] <= cus[i].come)	//客户无需等待
            window[j] = cus[i].come + cus[i].wait;
        else{							//客户需要等待,
            sum += window[j] - cus[i].come;	//这里同时间将提前到达的情况包含在内
            window[j] += cus[i].wait;
        }
    }
    if(n == 0)
        printf("0.0\n");
    else
        printf("%.1f\n", sum/60.0/n);
    return 0;
}

注意考虑n为0的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值