PAT 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

 题意:有n个人和k个窗口,窗口服务时间是08:00-17:00,求这些人的平均等待时间;注意在17.00以后到来的用户不计入平均时间计算和服务。

思路:这里把时间转化成秒,如果用结构体来表示hh:mm:ss这样的结构,比较起来很麻烦。把符合条件的用户放入到vector,窗口用结构体表示,end表示结束本次服务的时间。我们遍历每一个用户,每一个用户我们只需要去找最早空闲的窗口进行情况判断。要么正好用户比这个窗口要空闲的时间点还早,那肯定需要等待,这部分需要计算。如果是用户比窗口要空闲的时间点晚,只需要更新窗口结束本次服务的时间即可。

#include
using namespace std;

typedef struct
{
	int arrive,start,time;
}Person;

typedef struct
{
	int end = 8*3600;
}Window;

/*
把用户到达时间从小大大排序,然后遍历用户。找到窗口最早空闲的,然后处理程序逻辑。 
*/
bool cmp1(Person a, Person b) {
    return a.arrive < b.arrive;
}

vector persons;
vector windows; 

int main()
{
	int n,k;
	cin >> n >> k;
	int h,m,s,p;
	Person person;
	for(int i = 0;i < n;++i)
	{
		scanf("%d:%d:%d%d",&h,&m,&s,&p);
		person.arrive = h*3600+m*60+s;
                //超出服务时间的无需计算,直接舍弃即可
		if(person.arrive >= 17*3600+1) continue;
		person.time = (p*60 >= 3600 ? 3600 : p*60);
		persons.push_back(person);
	}
	//到达时间从小到大排序 
	sort(persons.begin(),persons.end(),cmp1); 
	Window window;
	for(int i = 0;i < k;++i)
		windows.push_back(window);
		
	int sum = 0;
	for(int i = 0;i < persons.size();++i)
	{
		
		int minendtime = 99999999,idx = -1;
		for(int j = 0;j < k;++j)
		{
			if(windows[j].end < minendtime)
			{
				minendtime = windows[j].end;
				idx = j;
			}
		}
		
		//早到了需要等待,更新该窗口结束时间 
		if(persons[i].arrive < minendtime)
		{
			
			sum += minendtime-persons[i].arrive;
			windows[idx].end += persons[i].time;
		}
		else
		{
			windows[idx].end = persons[i].arrive+persons[i].time;
		}
	}
	
	printf("%.1lf\n",sum*1.0/60/persons.size());
	return 0;
}

 

你可能感兴趣的:(pat甲级)