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.

按照到达时间排序,超过17:00的在输入时剔除,对于每一个人,选择最早空出来的窗口。

窗口可以使用优先队列优化。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct peo {
	int arrive, process;
};
bool cmp(peo p1, peo p2) { return p1.arrive < p2.arrive; }
int main() {
	int n, k, h, m, s, p;
	float ans = 0;
	cin >> n >> k;
	vector<peo>v; 
	vector<int>time(k);
	for (int i = 0; i < k; i++)time[i] = 8 * 3600;
	for (int i = 0; i < n; i++) {
		scanf("%d:%d:%d %d", &h, &m, &s, &p);
		if (h * 3600 + m * 60 + s <= 17 * 3600) 
			v.push_back({ h * 3600 + m * 60 + s ,p * 60 });
	}
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < v.size(); i++) {
		int early = time[0], pos = 0;
		for (int j = 0; j < k; j++) {//最早空闲窗口
			if (time[j] < early) {
				early = time[j];
				pos = j;
			}
		}
		if (v[i].arrive < time[pos]) {//顾客到达时没有空闲窗口
			ans += time[pos] - v[i].arrive;
			time[pos] += v[i].process;
		}
		else						//顾客到达时已经有空闲窗口,不需等待
			time[pos] = v[i].arrive + v[i].process;	
	}
	printf("%0.1f\n", ans / v.size() / 60);
	return 0;
}

这个银行它是这样的:17:00之前到达的所有顾客都会被服务,而并不是到17:00后服务完当前顾客之后就关门。所以假设从8:00-17:00,每隔一秒钟有一个顾客进来,服务时长60分钟,那这个银行这一年就不用干别的了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值