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个窗口,按照先后的顺序轮流到空的窗口服务,银行8点上班17点下班,一旦过了17点并不服务,最后我们要求出每个顾客wait的时间

解题思路:用一个结构存储已秒为单位的来的时间和处理的时间,我们只将符合的时间的顾客压入customer数组中,然后按照来的时间排序,接下来关键的就是找出结束最早的窗口,用2个循环为每个顾客找到结束最早的窗口,并更新窗口,这里的窗口用vector初始化为28800,也就是8点,每一个外循环更新一次window,如果窗口结束的时间比顾客来的时间还早就有wait时间,最后算平均的时间除以60再除以符合的顾客数量,这里还需要注意一点的是如果一个符合的都没有也就是0,除数不能为0,就要特别判断输出0.0就好了。

#include<bits/stdc++.h>
using namespace std;
struct bank{
	int coming,processing;
};
bool cmp(bank a,bank b)//将符合的顾客按照来的先后排序
{
	return a.coming<b.coming;
}
int main(void)
{
    int n,k,h,m,s,p;
    scanf("%d %d",&n,&k);
    vector<bank>customer;
    for(int i=0;i<n;i++)
    {
    	scanf("%d:%d:%d %d",&h,&m,&s,&p);
    	int temp=h*3600+m*60+s;
    	if(temp>61200) continue;//筛选符合的条件
    	customer.push_back({temp,p*60});
	}
	sort(customer.begin(),customer.end(),cmp);
	vector<int>window(k,28800);//直接将窗口初始为28800秒即8点
	double result=0;
	for(int i=0;i<customer.size();i++)
	{
		int mindex=0,minfinish=window[0];
        //找出最先结束的窗口
		for(int j=1;j<k;j++)
		{
			if(window[j]<minfinish)
			{
				mindex=j;
				minfinish=window[j];
			}
		}
        //更新window的值并将如果需要wait的加起来
	    if(window[mindex]<=customer[i].coming)
		{
			window[mindex]=customer[i].coming+customer[i].processing;
		}	
		else
		{
			result+=(window[mindex]-customer[i].coming);
			window[mindex]+=customer[i].processing;
		}
	}
    if(customer.size()==0) printf("0.0\n");
    else printf("%.1lf\n",result/60/customer.size());
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/83831907
今日推荐