PAT 1017 Queueing at Bank [模拟]

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], MMand 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

--------------------------------------这是题目和解题的分割线--------------------------------------

写一半没辙了,就参考了柳神的思路,模拟题是短板了(╥╯^╰╥) 

这道题有个很好用的小技巧,在需要大量进行时间上的加减法时,事先将它们转换为秒钟,会变得非常方便!

其实思路还是挺简单的,大概说说。结构体是肯定得有的,以便把到来时间和处理时间放一块儿,再按照到来时间排个序,这样就可以让大家有序排队等候啦。最后设置一个数组来模拟窗口,初始化窗口的结束时间为8:00,这样也方便加上没开门就来了的朋友们的等待时间。从数组中挑选出结束时间最早的,如果此时轮到的人的到来时间比这个结束时间还晚,那就说明无需等待,将该窗口的结束时间更新成这个人的到来时间+处理时间。如果到来时间比结束时间早,全员等待时间加一加,还有更新结束时间。

#include<cstdio>
#include<algorithm>
const int maxN = 10010;

using namespace std;

struct node
{
	int proTime; //处理时间 
	int startTime; //到来时间 
}cus[maxN];

bool cmp(node a,node b)
{
	return a.startTime<b.startTime;
}

int main()
{
	int i,j,n,w;
	int waitTime = 0,cntValid = 0;
	scanf("%d%d",&n,&w);
	for(i=0;i<n;i++)
	{
		int hh,mm,ss,t;
		scanf("%d:%d:%d %d",&hh,&mm,&ss,&t);
		//17:00之前来的才算有效人数 
		if(hh*3600+mm*60+ss<=17*3600)
		{
			//将和时间有关的变量通通转换为秒钟 
			cus[cntValid].proTime = t*60;  
			cus[cntValid].startTime = hh*3600+mm*60+ss;
			cntValid++;	
		}	
	}	
	sort(cus,cus+cntValid,cmp);
	/*for(i=0;i<cntValid;i++)
		printf("%d %d\n",cus[i].startTime,cus[i].proTime);
	printf("\n");*/
	int endTime[w+1];
	//初始化成开门时间8:00 
	fill(endTime,endTime+w+1,8*3600);
	for(i=0;i<cntValid;i++)
	{
		//最小窗口下标,最早结束时间
		//两个变量都需要初始化,毕竟很有可能进不去下面的if语句~ 
		int minV = 0,minT = endTime[0];
		for(j=1;j<w;j++)
		{
			//找到更早的结束时间,更新 
			if(endTime[j]<minT)
			{
				minV = j;
				minT = endTime[j];
			}
		}
		/*
		这里别滥用endTime[minV]和minT哈,这俩不一定相等!!!
		除非上面的循环进去过if语句才会相等!!! 
		*/ 
		//如果到来时间比最早的结束时间还晚 
		if(endTime[minV]<=cus[i].startTime)
			//更新该窗口的结束时间 
			endTime[minV] = cus[i].startTime+cus[i].proTime;		
		else
		{
			//否则加一加等待时间,以及别忘了更新结束时间 
			waitTime += endTime[minV] - cus[i].startTime;		
			endTime[minV] += cus[i].proTime;
		}
	}
	//输出要求是分钟,要除一个60 
	printf("%.1f\n",waitTime/60.0/cntValid);
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值