【PAT(甲级)】1017 Queueing at Bank

博客围绕银行排队问题展开,给出输入输出规范及示例。解题思路类似PAT甲级1014,需计算客户等待时间,将时间换算成秒更方便。还指出易错点,如17:00后到的人不参与计算,要先排序再处理早到顾客。最后给出C++代码求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (≤104) - 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

解题思路:

排队类似于PAT甲级1014 Waiting in Line。只是变成每个黄线内的每个窗口前只能有一个人。

只是变了一下,客户的等待时间,就是等于客户到达的时间减去客户实际被服务的时间。也就是客户前一个人的离开时间。

具体思路可以看http://t.csdn.cn/vtqF3

其中时间的换算,最好全部换算成秒来计算,这样子会方便很多。早于8点的也就是小于8*3600秒的,记住要8点才是开始时间。在等待时间中加上顾客相差到8点的时间,再把开始时间改成8点即可。

易错点:

1. 17:00以后到的人不仅不被提供服务,而且也不算在等待的人数当中。每当有一个人在17点以后到,平均时间的人数就要减去1.

2. 记得要先按照时间顺序将顾客排好队之后,再将早于8点到的顾客开始时间换算成8点,不然会出错。

代码:

#include<bits/stdc++.h>
using namespace std;
int N,K;
double toltime=0;
typedef struct Customer{//自定义结构来存储顾客信息 
	int start;
	int time;
	int end;
};

void till_8(Customer a[]){//将所有早于8点到的人,换算成8点到 
	for(int i=0;i<N;i++){
		if(a[i].start<8*3600){
			toltime+=8*3600-a[i].start;
			a[i].start=8*3600;
		}
		a[i].end = a[i].start+a[i].time;
	}
}

bool cmp(Customer a,Customer b){//按时间先来后到排序排序 
	return a.start<b.start;
} 

int main(){
	cin>>N>>K;
	Customer C[N];
	Customer window[K];
	for(int i=0;i<N;i++){
		int HH,MM,SS,time;
		scanf("%d:%d:%d %d",&HH,&MM,&SS,&time);
		C[i].start=HH*3600+MM*60+SS; //统一将时间换算成秒 
		C[i].time=time*60;
	}
	sort(C,C+N,cmp);
	till_8(C);
	int tolmen=N;
	for(int i=0;i<N;i++){
		if(C[i].start>17*3600){//大于17:00:00到的顾客不服务 
			tolmen--;//总等待人数减一 
			continue;
		}
		if(i<K) window[i]=C[i];
		else{
			int min=99999999;
			int temp=0;
			for(int j=0;j<K;j++){
				if(window[j].end<min){
					min=window[j].end;
					temp=j;
				}
			}
			if(C[i].start<=window[temp].end){
				toltime+=window[temp].end-C[i].start;
				C[i].start = window[temp].end;
				C[i].end = C[i].start+C[i].time;
				window[temp]=C[i];
			}
			else{
				window[temp]=C[i];
			}
		}
	}
	printf("%.1f",toltime/60/tolmen);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值