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

时间:2021-07-26 17:53:32
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中空缺的位置,计算这个人的等待时间
PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)
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 ;
}