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 (<=10000) - 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

思路

有N个人,K个窗口,一条黄线,先来先到原则。算客户平均等待时间

①8:00以前到的要等到8:00

②17:00以后到的就不能服务了,不算进平均数

③超过60min的服务统一压缩到60min


C++:

#include "cstdio"
#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;

struct customer
{
	customer(){
		this->MM=0;
		this->HH=0;
		this->SS=0;
		this->pro_time=0;
		this->wait_time=0;
	}
	int MM,HH,SS;//到达时间
	int pro_time;//处理时间
	int wait_time;//等待时间
};

bool cmp(customer a,customer b){
	if (a.HH!=b.HH)
	{
		return a.HH<b.HH;
	}else if (a.MM!=b.MM)
	{
		return a.MM<b.MM;
	}else
	{
		return a.SS<b.SS;
	}
}

int waitTime(customer a,customer b){
	int time=0;
	while (a.HH>b.HH||a.MM>b.MM||a.SS>b.SS)
	{
		time++;
		b.SS++;
		if (b.SS>=60)
		{
			b.SS=0;
			b.MM++;
		}
		if (b.MM>=60)
		{
			b.MM=0;
			b.HH++;
		}
	}
	return time;//秒数
}
int main(){
	int n,k;
	int index=0;
	cin>>n>>k;
	vector<customer> data(n);
	vector<int> window(k);
	fill(window.begin(),window.end(),0);
	customer begin;//bank营业时间
	begin.HH=8;
	customer end;//bank结束营业时间
	end.HH=17;
	for (int i=0;i<n;i++)
	{
		index++;
		scanf("%d:%d:%d %d",&data[i].HH,&data[i].MM,&data[i].SS,&data[i].pro_time);
		if (data[i].pro_time>60)
		{
			data[i].pro_time=60;
		}
		if (data[i].HH<8)
		{
			int time=waitTime(begin,data[i]);
			data[i].wait_time=time;//比开门时间早到的客户会加上等待时间
		}
		if (data[i].HH>=17)
		{
			int time=waitTime(data[i],end);
			if (time>0)
			{
				index--;
			}
		}
	}
	sort(data.begin(),data.end(),cmp);
	int now=0;//营业时长
	for (int i=0;i<index;i++)
	{
		//找到最早结束的那个窗口
		int index=0;
		int minfinish=window[0];
		for (int j=1;j<k;j++)
		{
			if(window[j]<minfinish){
				minfinish=window[j];
				index=j;
			}
		}
		now=minfinish;
		if (data[i].HH<8)//这个人是提早到的
		{
			data[i].wait_time+=now;
			window[index]+=data[i].pro_time*60;//把这个人放到最先结束的那个窗口
		}else{
			if (minfinish<=waitTime(data[i],begin))
			{
				now=waitTime(data[i],begin);
				window[index]=now+data[i].pro_time*60;//把这个人放到最先结束的那个窗口
			}else
			{
			data[i].wait_time+=now-waitTime(data[i],begin);//现在的时间减去到达的时间
			window[index]+=data[i].pro_time*60;//把这个人放到最先结束的那个窗口
			}
		}
	}
	int totalTime=0;
	for (int i=0;i<index;i++)
	{
		totalTime+=data[i].wait_time;
	}
	if (index==0)
	{
		printf("0.0");
	}else{
		printf("%.1f",totalTime/60.0/index);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/ysq96/article/details/80463508