题目如下:

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 (<=10000) - 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

这是一道事件模拟的问题,与前面的排队问题输出业务完成时间不同,本题要求输出每位顾客的等待时间。

我自己没有想到简洁的解决方法,直到看到了sunbaigui的方法。

他的方法核心思路是给每个窗口记录一个时间,代表当前服务的顾客结束的时间,每次取出最早结束服务的窗口,如果等待队列中最前面的顾客到达的时间比这个时间早,说明在等待中,一直要等到服务结束,因此等待时间等于这两个时间的差,此时,窗口的服务结束时间应该被更新为原来的时间(即新顾客开始服务的时间)加上新顾客服务的时间;如果等待队列中最前面的顾客到达时间大于窗口结束服务的时间,说明无需等待,直接服务,这时候窗口的结束服务时间应该被更新为新顾客的到达时间加上服务的持续时间。

为了得到队列最前面的顾客和最早结束服务的窗口,创建两个优先队列,分别管理所有顾客和所有窗口。

STL中的优先队列priority_queue是最大堆容器,默认使用<来比较元素,显然本题目中顾客和窗口的存取规律满足最小堆,因此重载<来实现最小堆。

虽然时间包含时、分、秒,为了简便,都以秒的总和来存储,并且取时间基准为8:00:00对应的秒数28800,因为服务最晚到17:00:01之前,因此取时间上限为61201,在初始化时把所有窗口的服务结束时间设定为28800,也就是说在8:00:00之前到达的顾客需要等待到8:00:00才能接受服务。

为了排除不能服务的人加入平均时间计算,应该在遍历顾客队列时设定一个变量cnt,记录能够被服务的人的数量,在顾客到达时间大于等于时间上限时,及时中断循环避免时间浪费。

#include<iostream>
#include<iomanip>
#include<queue>
#include<stdio.h> using namespace std; struct Person{
int hour;
int minute;
int second;
int last;
int total; Person(int _h, int _m, int _s, int _l) : hour(_h), minute(_m), second(_s), last(_l * 60){
total = _h * 3600 + _m *60 + _s;
} bool operator < (const Person& p) const { if( total > p.total ){
return true;
}else{
return false;
} } }; struct Window{
int total; Window(int _t):total(_t){} bool operator < (const Window& w) const{ if( total > w.total ){
return true;
}else{
return false;
} } }; int main(){ priority_queue<Person> persons;
priority_queue<Window> windows; int N,K;
cin >> N >> K; int clk_base = 28800;
int clk_limit = 61201;
int wait_total = 0; for(int i = 0; i < K; i++){
windows.push(Window(clk_base));
} int h,m,s,l;
for(int i = 0; i < N; i++){
scanf("%d:%d:%d%d",&h,&m,&s,&l);
persons.push(Person(h,m,s,l));
} int cnt = 0;
Person p = Person(0,0,0,0);
Window w = Window(0);
while(!persons.empty()){ p = persons.top();
persons.pop();
if(p.total >= clk_limit) break;
cnt++;
w = windows.top();
windows.pop();
if(p.total < w.total){
wait_total += w.total - p.total;
w.total += p.last;
}else{
w.total = p.total + p.last;
}
windows.push(w); } printf("%0.1f",wait_total / cnt / 60.0); return 0; }

1017. Queueing at Bank (25) - priority_queuet的更多相关文章

  1. PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)

    1017 Queueing at Bank (25 分)   Suppose a bank has K windows open for service. There is a yellow line ...

  2. 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 ...

  3. 1017 Queueing at Bank (25)(25 point(s))

    problem Suppose a bank has K windows open for service. There is a yellow line in front of the window ...

  4. PAT 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 ...

  5. 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 ...

  6. PAT (Advanced Level) 1017. Queueing at Bank (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  7. PAT甲题题解-1017. Queueing at Bank (25)-模拟

    有n个客户和k个窗口,给出n个客户的到达时间和需要的时长有空闲的窗口就去办理,没有的话就需要等待,求客户的平均时长.如果在8点前来的,就需要等到8点.如果17点以后来的,则不会被服务,无需考虑. 按客 ...

  8. 【PAT甲级】1017 Queueing at Bank (25 分)

    题意: 输入两个正整数N,K(N<=10000,k<=100)分别表示用户的数量以及银行柜台的数量,接下来N行输入一个字符串(格式为HH:MM:SS)和一个正整数,分别表示一位用户到达银行 ...

  9. PAT 1017 Queueing at Bank[一般]

    1017 Queueing at Bank (25)(25 分)提问 Suppose a bank has K windows open for service. There is a yellow ...

随机推荐

  1. sqlserver 取日期年份月份

    select convert(varchar(10),datepart(YYYY,a.fssj)) as years,--得到年份convert(varchar(10),datepart(mm,a.f ...

  2. python学习之路网络编程篇(第四篇)- 续

    Memcache简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...

  3. Python3 OS 文件/目录方法

    os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作 ...

  4. 使用Boost program_options控制程序输入

    简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...

  5. Bootstrap3 表格-状态类

    通过这些状态类可以为行或单元格设置颜色. .active---鼠标悬停在行或单元格上时所设置的颜色 .success--–标识成功或积极的动作 .info----标识普通的提示信息或动作 .warni ...

  6. android 自定义view之选座功能

    效果图: 界面比较粗糙,主要看原理. 这个界面主要包括以下几部分 1.座位 2.左边的排数 3.左上方的缩略图 4.缩略图中的红色区域 5.手指移动时跟随移动 6.两个手指缩放时跟随缩放 主要技术点 ...

  7. 微信小程序基础之input输入框控件

    今天主要详写一下微信小程序中的Input输入框控件,输入框在程序中是最常见的,登录,注册,获取搜索框中的内容等等都需要,同时,还需要设置不同样式的输入框,今天的代码中都要相应的使用. input输入框 ...

  8. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  9. RxJava(八)concat符操作处理多数据源

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51568562 本文出自:[余志强的博客] 一.concat操作符概述 ...

  10. EJB_开发单表映射的实体bean

    开发单表映射的实体bean 实体bean 它属于java持久化规范(JPA)里的技术,实体bean通过元数据在Javabean和数据库表之间建立起映射关系,然后Java程序员就可以随心所欲的使用面向对 ...