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
结尾无空行

解析:
很难的题目,感觉做题的自信要没了,痛苦。

银行排队,求平均等待时间,难点在于分析和坑点还有排队的思路。

坑点分析:八点之前来的顾客,等待的时间也得算上
17 点之后来的顾客直接 pass 掉
建议都先转换成 秒,最后再化为小时,这样能减少错误,避开小数点
注意最后除以的人数不是 n,而是在有效的时间来的人数

注意先排序,先来后到嘛

排队分析:
一共有 k 个窗口,我们假定每个窗口存储的是业务结束的时间,定义数组,循环每一个顾客,找到当前最早结束的窗口
如果结束时间比顾客到达时间早,说明,顾客来了以后直接进入窗口,无需等待,同时更新结束时间为 (顾客到达时间 + 处理事务时间)
如果结束的时间比来的时间晚,先计算等待的时间为 (结束时间 - 到达时间)
然后更新窗口的结束时间为 (上一次结束的时间 + 处理事务的时间)

#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<cstring>
using namespace std;
const int N = 10001;
struct Person {
	int at;
	int time;
}p[N];
bool cmp(Person a, Person b) {
	if(a.at == b.at) {
		return a.time < b.time;
	}
	return a.at < b.at;
}
int main() {
	int n, k;
	cin >> n >> k;
	int windows[101] = {0};
	int index = 0;
	int res = 0;
	for (int i = 0; i < n; i++) {
		int hh, mm, ss, time;
		scanf("%d:%d:%d %d", &hh, &mm, &ss, &time);
		if (hh == 17 && (mm > 0 || ss > 0)) {// 来晚了,过期不候
			continue;
		}
		if (time > 60) {
			time = 60;
		}
		p[index].at = (hh - 8) * 3600 + mm * 60 + ss;// 到达时间 
		p[index++].time = time * 60;
	}
	if (index == 0) {// 没人,直接输出,注意保留一位小数
		cout << "0.0" << endl;
		return 0;
	}
	sort(p, p + index, cmp);// 按到达时间排序
	for (int i = 0; p[i].at < 0; i++) {// 计算早来的人的等待时间
		res += 0 - p[i].at;
		p[i].at = 0;
	} 
	// 开始排队啦!!
	for (int i = 0; i < index; i++) {
	//  找到当前最早结束的窗口
		int minT = windows[0], t = 0;
		for (int j = 1; j < k; j++) {
			if (minT > windows[j]) {
				minT = windows[j];
				t = j;
			}
		}
		// 判断顾客的到达时间与结束时间的关系
		if (windows[t] <= p[i].at) {
			windows[t] = p[i].at + p[i].time;
		} else {
			res += windows[t] - p[i].at;
			windows[t] += p[i].time;
		}
	}
	printf("%.1lf", res / 60.0 / (index * 1.0));
	return 0;
}


上一篇:2021-11-16[每日一题]391. 完美矩形__python3


下一篇:具体解释EBS接口开发之WIP模块接口