PAT甲级1017 Queueing at Bank

1017 Queueing at Bank

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、等待时间的处理:等待时间需要更新的情况是:
(1)顾客早于八点到
(2)窗口空出来的时间晚于下一顾客到的时间
(3)顾客到了,但是银行下班了,顾客到达直到五点的这段时间:这一点没考虑到,一开始认为当窗口处理到五点后的话后面的顾客都可以忽略了,其实不是的,测试用例可以参考https://www.freesion.com/article/7469366874/

教训:PAT难点之一在于答案错误时你不知道用例情况,得自己去想可能出错的用例,尤其是特殊用例(但像这一题用例格式比较复杂的情况下自己想一个出来测试也是比较难的)

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
ans
8.2

3 3
08:01:00 60
08:00:00 30
08:00:02 2
ans
0.0

3 3
07:55:00 16
17:00:01 2
07:59:59 15
ans
2.5

3 3
16:59:59 20
17:00:01 2
17:00:00 10
ans
0.0

3 3
17:00:00 10
17:01:00 2
17:03:00 9
ans
0.0



4 1
16:55:00 4
16:58:00 10
16:59:00 2
17:00:00 10
ans
5.5


3 1
16:55:00 3
16:56:00 2
16:59:00 10
ans
1.0

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

int conver(const string &time){
    int ans = 0;
    int temp = 0;
    for(int i = 0;i < time.size();++i){
        if(time[i] != ':'){
            temp = temp * 10 + time[i] - '0';
        }
        else{
            ans = ans * 60 + temp;
            temp = 0;
        }
    }
    ans = ans * 60 + temp;
    return ans;
}
struct customer{
    int arrive,process;
    bool operator <(const customer &x)const{
        return arrive < x.arrive;
    }
};
int main(){
    int n_win,n_cus;
    cin >> n_cus >> n_win;
    vector<int> window(n_win);
    vector<customer> cus;
    for(int i = 0;i < n_cus;++i){
        string temp;
        int process;
        cin >> temp >> process;
        int t = conver(temp);
        if(t > 61200){
            continue;
        }
        customer L;
        L.arrive = conver(temp);
        L.process = process * 60;
        cus.push_back(L);
    }
    sort(cus.begin(),cus.end());
    int index = 0;
    int waiting_time = 0;
    int j = cus.size();
    for(int i = 0;i < n_win;++i){
        if(index < j){
            if(cus[index].arrive < 28800){
                waiting_time += 28800 - cus[index].arrive;
                cus[index].arrive = 28800;
            }
            window[i] = cus[index].arrive + cus[index].process;
            index++;
        }
    }
    sort(window.begin(),window.end());
    while(index < j){
        if(cus[index].arrive < 28800){
            waiting_time += 28800 - cus[index].arrive;
            cus[index].arrive = 28800;
        }
        if(window[0] > cus[index].arrive){
            waiting_time += window[0] - cus[index].arrive;
            window[0] = window[0] + cus[index].process;
        }
        else{
            window[0] = cus[index].arrive + cus[index].process;
        }
        sort(window.begin(),window.end());
        index++;
    }
    if(j == 0){
        cout << 0.0 << endl;
    }
    else{
        float res = waiting_time / (float)j;
        res /= 60;
        printf("%.1f\n",res);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值