1017 Queueing at Bank

题目来源:PAT (Advanced Level) Practice

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

words:

customer 用户,顾客        occupy 占用,使用

题意:

给定n个用户和k的窗口,模拟用户进行窗口服务的时间过程(等待时间+服务时间),计算所有用户的平均等待时间;

思路:

1. 用结构体数组p[]把用户的到达时间和服务时长(超过60分钟按60分钟存储)存储起来,存储时将时分秒制的时间统一转化为秒来存储(不然后续计算会很麻烦);

2. 用数组window[]表示每个窗口的服务结束时间,即可接受下一次服务的开始时间,初始化为早上8点(8*3600);

3. 将用户按到达时间的先后顺序进行排序和服务,然后每次选取window[]中服务结束时间最早的窗口进行服务(可通过sort()排序来实现),并计算用户的等待时间和重置该窗口的服务结束时间

4. 通过所有用户的等待时间求出平均等待时间并输出;

//PAT ad 1017 Queueing at Bank
#include <iostream> 
using namespace std;
#include <algorithm>
#include <iomanip>
#define N 10005
#define K 105

struct abc
{
	int hms;	//时间,以秒计时 
	int minute;		//时长,分钟 
}p[N];

int window[K]; 	//每个窗户服务结束时间 


bool cmp(abc x,abc y)
{
	return x.hms<y.hms;
}

int startTime=8*3600; 	//起始时间 
int endTime=17*3600;	//结束时间 

int main()
{
	int n,k,i;
	cin>>n>>k;
	int hh,mm,ss,minute;
	int c=0;	//17点之前来的人数 
	for(i=0;i<n;i++)		//输入 
	{
		scanf("%d:%d:%d %d",&hh,&mm,&ss,&minute);
		int time=hh*3600+mm*60+ss;		//转化为秒计时 
		if(time<=endTime)
		{
			p[c].hms=time;
			p[c].minute=min(minute,60);
			c++;
		}
	}
	
	sort(p,p+c,cmp);		//排序,按照时间的先后顺序 
		
	fill(window,window+k,startTime); 	//初始化窗口服务时间 
	
	
	int sum=0;	//总的等待时间 	
	for(i=0;i<c;i++)
	{			
		sort(window,window+k);	//按服务结束时间排序 
		int t=window[0]-p[i].hms;	//等待时间

		if(t>=0) 			//需要等待 
		{
			sum+=t;		 
			window[0]+=p[i].minute*60;		//重置窗口的服务结束时间 
		}
		else
			window[0]=p[i].hms+p[i].minute*60;	//重置窗口的服务结束时间 
	}
			
	cout<<fixed<<setprecision(1)<<sum/60.0/c<<endl;
	
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值