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 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 (≤10
​4
​​ ) - 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

题目大意:

排队问题,有k个窗口,给出n个顾客的到达时间和办理业务的时间,所有的顾客按照到达的先后顺序排成一队,当有窗户空闲,最前面的顾客上前办理。求顾客平均等待时间。注意:银行8点开门,17点之后到达的人不能办理,不计入总时间,

题目解析:

结构体node记录顾客到达时间和处理时间,用window[k]数组记录窗口办理业务的完成时间,初值设为8点。顾客按照到达时间从小到大遍历,每次选出最早办理完成的窗口,如果窗口完成时间早于顾客到达时间,总等待时间增加差值,更新窗口完成时间;反之只用更新窗口完成时间。

具体代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node {
	int come_time;
	int process_time;
}customer[10010];
int window[110];
bool cmp(node x,node y){
	return x.come_time<y.come_time;
}
int main() {
	int n,k;
	cin>>n>>k;
	int hh,mm,ss,process_time;
	for(int i=0; i<n; i++) {
		scanf("%d:%d:%d %d\n",&hh,&mm,&ss,&process_time);
		customer[i].come_time=hh*3600+mm*60+ss;
		customer[i].process_time=process_time*60;
	}
	for(int i=0;i<k;i++)
		window[i]=28800;
	sort(customer,customer+n,cmp);
	int count_time=0,count_num=0;
	for(int i=0;i<n;i++){
		if(customer[i].come_time<=61200){
			int min_index=0,min_time=window[0];
			for(int j=1;j<k;j++)
				if(window[j]<min_time){
					min_time=window[j];
					min_index=j;
				}
			if(customer[i].come_time<min_time){
				count_time+=min_time-customer[i].come_time;
				window[min_index]+=customer[i].process_time;
			}else{
				window[min_index]=customer[i].come_time+customer[i].process_time;
			}
			count_num++;
		}
	}
	if(count_num==0)
		printf("0.0");
	else
		printf("%.1f",(double)count_time/count_num/60);
	return 0;
}
全部评论

相关推荐

02-12 20:22
重庆大学 Java
字节暑期刚入职四天,因为是年前,所以很多正职都放假走了,也就没有给我分配mt,然后有一个老哥在我来的时候给我发了一个landing手册,然后还有关于部门业务的白皮书,还有一些业务代码。然后本人是java面的,进来第一次接触go语言&nbsp;前面几天熟悉了一下go的语法和go的框架,可以读但是还不太会写,然后业务白皮书也看的很头疼,包括landing手册里要了解的很多东西说实话我看文档真的快看死了,一个嵌套一个,问题是我还完全不知道咋用这个我了解的东西,还有就是那个项目代码,那个老哥喊我去写写单测,熟悉一下go的语法,但也进行的很困难(这是我第一段实习,之前都是springboot那一套,真不太熟悉这个)想问问大家的建议,就是我从现在开始到在开年回来之前应该做些什么,我目前就一个想法&nbsp;就是复现一个landing手册上的go框架小项目&nbsp;就是相当于帮自己锻炼锻炼怎么写go&nbsp;或者各位大佬有没有更好的锻炼go语法的建议还有就是大家都在说vibe&nbsp;coding,那我应该怎么锻炼自己使用ai的能力,感觉我除了给一些需求然后它给我生成代码,好像就没别的用法了,那些什么工作流、拆解、skill啥的都不知道从哪一个地方开始,包括我现在正在实习,不知道精力该怎么分配,去网上想找找关于agent开发的一些学习流程,说实话,众说纷纭,有的是从python开始打基础然后系统学那些rag&nbsp;prompt&nbsp;langchain&nbsp;mcp等等,有的是说直接找一个github上的ai项目然后反复问ai,我确实有点迷茫,恳求各位大佬能留下你们宝贵的建议,我一定认真反复深刻学习有一说一&nbsp;我觉得字节饭挺好吃的!
双非后端失败第N人:1. go语言我建议你让ai带着你先把基本语法速通了,然后再去用go重新刷你以前刷过的leetcode,这样熟悉起来很快 2. 直接看你们组go项目,里面用***比较复杂,然后把每一个语法现象都喂给ai,一点点看
字节跳动公司福利 1371人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务