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 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 (≤) - the total number of customers, and K (≤) - 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
题目大意:
这题主要是模拟客户等待的平均时间,按照先来先服务的原则,并且在17:00:01后到来的客户无法进行服务,每个窗口只能有一个人。
17:00:00及以前到的一定要继续服务完。
思路:
1.统一都用s计,8点是28800s,17点是61200s
2.按来的时间排序,数组中只要存两个数即可:s开始服务的时间,t服务所需的时间。
3.怎么算每个人等了多久呢(这个人等的时间不包括他自己办理业务的时间)
第一批人(k个人)的等待时间:如果早于8点,那么等待时间为 28800-s
晚于 ,他来的时候还有位置空着,等待时间为0
之后的人的等待时间,是有规律的。
先在q中挑出mint,谁办完了,就把位置让掉,然后把等待的人群a里面最前的那个人挑出来,放到q中空缺的位置,计算这个人的等待时间

4.注意!考虑没有人的情况,此时分母为0!!!
一遍过AC代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int s;
int t;
}a[];//在等待的人们
node q[];//在办业务的人们
bool cmp(node &x,node &y){
return x.s<y.s;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=;i<=n;i++){
int h,m,s;
double t;
scanf("%2d:%2d:%2d %lf",&h,&m,&s,&t);
a[i].s=h*+m*+s;//开始的时刻
a[i].t=int(t*);//需要花多久
if(a[i].t>){//每个人最多60分钟
a[i].t=;
}
}
sort(a+,a+n+,cmp);
/*for(int i=1;i<=n;i++){
cout<<a[i].s<<" "<<a[i].t<<endl;
} */ //初始化
double sum=;//等待的总时间
int p_sum=;//人数
for(int i=;i<=n;i++)
{
if(i<=k)
{
p_sum++;
if(a[i].s<=){
sum+=-a[i].s;
//cout<<"第"<<a[i].s<<"人"<<"等了 "<<28800-a[i].s<<endl;
a[i].s = ;
}
q[i].t=a[i].t;
q[i].s=a[i].s;
}else{
q[i].s=;
}
} int p=k+;//p代表下一个轮到谁了
int mint=;
while(p<=n){//还有人在等着办业务
if(a[p].s>) {//超过17点了
break;
}
//挑出队列中结束时间最早的时刻mint
int mint=;
for(int i=;i<=k;i++){
if(q[i].s+q[i].t<mint)
{
mint=q[i].s+q[i].t;
}
}
//cout<<"mint "<<mint<<endl;
//跳到mint这一时刻
for(int i=;i<=k;i++){
if(q[i].s+q[i].t==mint){
//第qi个人换成第p个人
q[i].s=a[p].s;
q[i].t=a[p++].t;
p_sum++;
if(mint>q[i].s){
sum+=mint-q[i].s;
//cout<<"第"<<q[i].s<<"人"<<"等了 "<<mint-q[i].s<<endl;
q[i].s=mint;//这个人的开始时间不再是原来的了
}
}
}
}
if(p_sum!=){
sum=sum/p_sum;
sum=sum/;
}
else{//特殊情况!
sum=;
}
printf("%.1lf\n",sum);
return ;
}
PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)的更多相关文章
- PAT甲级1017. Queueing at Bank
PAT甲级1017. Queueing at Bank 题意: 假设一家银行有K台开放服务.窗前有一条黄线,将等候区分为两部分.所有的客户都必须在黄线后面排队,直到他/她轮到服务,并有一个可用的窗口. ...
- 【PAT甲级】1017 Queueing at Bank (25 分)
题意: 输入两个正整数N,K(N<=10000,k<=100)分别表示用户的数量以及银行柜台的数量,接下来N行输入一个字符串(格式为HH:MM:SS)和一个正整数,分别表示一位用户到达银行 ...
- PAT 甲级 1017 Queueing at Bank
https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968 Suppose a bank has K w ...
- 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 ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- 用js刷剑指offer(调整数组顺序使奇数位于偶数前面)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 牛客网链接 js代码 ...
- ArrayList 和 Vector 的区别是什么?(未完成)
ArrayList 和 Vector 的区别是什么?(未完成)
- c++对象模型和RTTI(runtime type information)
在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...
- JAVA遇见HTML——JSP篇(JavaBeans)
1.像使用普通java类一样,创建javabean实例,利用构造方法创建实例 跟表单关联,“*”表示根据名称来进行匹配,就是根据表单所提交过来的参数的名字和Javabean当中的属性名字来进行一一匹配 ...
- 甘特图、Data Editors控件新玩法—DevExpress WPF v19.2
通过DevExpress WPF Controls,你能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案. 无论是Office办公软件的衍 ...
- django.db.utils.OperationalError: (1050, "Table 'article_category' already exists")
(转自:https://blog.csdn.net/huanhuanq1209/article/details/77884014) 执行manage.py makemigrations 未提示错误信息 ...
- BZOJ 3903 反垄断 (最大流推的结论题)
题目 中文题目,不解释: BZOJ传送门 分析 这道题BZOJ上也只有几个人过-奇怪了 下面是正解 原问题为一个二分图边染色问题.首先考虑最好情况.最理想情况的分配为:设一个点xxx的度为dgr(x) ...
- 基本数据类型-MySQL
整型: TINYINT 最小 1个字节 -128~127 0~255 SMALLINT 较小 2个字节 -32768~32767 0~65535 MEDIUMINT 中等大小 3个字节 略 INT ...
- C# 使用多线程,在关闭窗体时 怎么关闭窗体的所有线程,使程序退出。
this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: Application.Exit(); 强制所有消息中止,退出 ...
- python 迭代工具
names = ['anne', 'beth', 'george', 'damon'] ages = [, , , ] for name,age in zip(names,ages): #print( ...