1017 Queueing at Bank (25分)【模拟,结构体排序】

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个窗口。并且告诉你n个客户的到达时间,以及每个客户的服务时长。每个客户在没有空窗口的情况下统一在黄线外等候,同时银行8:00开门,17:00关门,要求你求出所有客户的平均等待时间。

1.定义一个结构体,保存客户的到达时间和服务时长(全部转换为秒为单位)。

2.在存储客户数据时,如果到达时间大于61200秒,则不需要保存(此时已超过17点)。

3.对结构体进行排序,到达时间早的在前面,这样构成一个客户队列。

4.用一个window数组表示窗口,起始值为28800(8点),对于前面的客户队列中的每一个客户,遍历所有窗口早到其中完成时间最早的,如果这个最早完成窗口的完成时间比队头的客户到达时间要早,则客户无需等待;如果完成时间比客户到达时间晚,则客户需等待,记录等待时间后,再让当前队头客户进入窗口。

#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;

struct Cust {
	int time;   //将每个人到达的时间转化为秒
	int servetime;
}cus[10010];

int window[110];

int cmp(Cust a, Cust b)     //按time由早到晚排序
{
	return a.time < b.time;
}

int main()
{
	int n, k;
	cin >> n >> k;
	int cnt = 0;       //记录真正需要等待的客户数
	for (int i = 0; i < n; i++)
	{
		int h, m, s, time;
		scanf("%d:%d:%d %d", &h, &m, &s, &time);
		int tosecond =  h * 3600 + m * 60 + s;
		if (tosecond > 61200)    //如果超过了17:00不用存了;
			continue;		
		cus[cnt].servetime = time * 60;
		cus[cnt].time = tosecond;
		cnt++;
	}
	sort(cus, cus + cnt, cmp);
	fill(window, window + k, 28800);   //28800是8:00转换为秒
	double sum = 0;
	for (int i = 0; i < cnt; i++)
	{
		int winpos = 0;
		int mintime = window[0];
		for (int j = 1; j < k; j++)        //找出时间最早的让下一个等待的客户进去
		{
			if (window[j] < mintime)
			{
				winpos = j;
				mintime = window[j];
			}
		}
		if (cus[i].time >= mintime)
			window[winpos] = cus[i].time + cus[i].servetime;
		else
		{
			sum += (mintime - cus[i].time);
			window[winpos] += cus[i].servetime;
		}

	}		
	if (sum == 0)   //注意,当客户数等于0的情况,此时不能做除数
		cout << "0.0" << endl;
	else
		printf("%.1f\n", sum*1.0 / 60.0 / cnt);
	return 0;
}

 

posted @ 2020-02-29 10:55  Hu_YaYa  阅读(19)  评论(0)    收藏  举报