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 HHis 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 ...
随机推荐
- vue sample
<template> <section> <header class="top_tips"> <span class="num_ ...
- 3.使用webpack配置文件webpack.confg.js配置打包文件的入口和出口
在项目根目录下新建webpack.config.js文件 webpack.config.js文件配置如下: // Node的路径操作使用的是path模块 const path=require('pat ...
- 现代浏览器性能优化-CSS篇
我来填坑了,CSS篇终于写出来了,如果你没看过前面的JS篇,可以在这里观看. 众所周知,CSS的加载会阻塞浏览器渲染或是引起浏览器重绘,目前业界普遍推荐把CSS放到<head>中,防止在C ...
- nginx location if 的匹配规则
cation匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹配,不区分大小写^~ #^~表示普通字符匹配,不是正则匹配.如果该选项匹配,只匹配该选 ...
- maven+struts2例子
本文在开发第一个maven示例的基础上进行扩展. 第一个maven示例目录结构如下: 扩展后目录为: 打开pom.xml 在WEB-INF文件夹下新建web.xml: <?xml version ...
- 项目:JS实现简易计算器案例
组件化网页开发下的: 步骤一:让页面动起来的JavaScript深入讲解 的 项目:JS实现简易计算器案例
- Flutter 踩坑集
1.Flutter Packages Get 一直重试或一直失败的问题 翻车原因:万恶之源-----天朝的长城防火墙 解决方法 详见:https://flutter.dev/community/chi ...
- Gradle 如何打包 Spring Boot 可执行 JAR
如何在 Gradle 中配置一个项目可以打包为 Spring Boot 可执行 Jar? 你首先需要添加到 org.springframework.boot 到插件中: 例如下面的代码: plugin ...
- Codeforces Round #572 (Div. 2)
目录 Contest Info Solutions A. Keanu Reeves B. Number Circle C. Candies! D1. Add on a Tree D2. Add on ...
- LOJ166 拉格朗日插值 2【卷积,NTT】
题目链接:LOJ 题目描述:输入多项式的次数$n$,一个整数$m$和$f(0),f(1),f(2),\ldots,f(n)$,输出$f(m),f(m+1),f(m+2),\ldots,f(m+n)$ ...