【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

题目大意:

假设银行有K个窗口为服务打开。窗户前面有一条黄线,把等候区分成两部分。所有的顾客都要在黄线后排队等候,直到轮到他/她时才有空位。假设没有一个客户可以占用一个窗口超过1小时。现在给定每个客户的到达时间T和处理时间P(单位是分钟),你应该告诉所有客户的平均等待时间。假设没有两个客户同时到达。注意,银行的营业时间是8点到17点。早到者必须排队等候至08:00,晚到者(17:00:01或17:00:01以后)不计算在内。

解题报告:

拿一个优先队列维护可用窗口就可以了,然后因为题意是按照到来的先后顺序排队等待的,所以不需要考虑(7:00:00 25)的顾客和(7:30:00 5)的顾客占用窗口的先后顺序问题,因为如果是按照最优方案的话肯定是先安排(7:30:00 5)的顾客更划算,因为他服务时间短,只有5分钟。复杂度是O(n*log(k)),如果不用优先队列的话复杂度是O(n*k),也可以AC。

甚至这题从8:00:00开始一秒一秒的模拟到18:00:00也可以,复杂度是O(36000*k)。可以思考一下为啥到18点而不是17点

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,k;
priority_queue<int,vector<int>,greater<int> > pq;
struct Node {
	int t;
	int service;
} R[MAX];
int hh,mm,ss;
bool cmp(Node a,Node b) {
	return a.t < b.t;
}
const int ed = 17*3600;//17:00:00 
const int st = 8 *3600;// 8:00:00 
int main()
{
	cin>>n>>k;
	for(int i = 1; i<=n; i++) {
		scanf("%d:%d:%d%d",&hh,&mm,&ss,&R[i].service);
		R[i].service *= 60;
		R[i].t =  hh*3600 + mm*60 + ss;
	}
	sort(R+1,R+n+1,cmp);
	int wait = 0,all = 0;
	for(int i = 1; i<=n; i++) {
		if(R[i].t >= ed) break;
		all++;
		if(R[i].t < st) wait += st - R[i].t,R[i].t = st;
		if(pq.size() < k) pq.push(R[i].t + R[i].service);
		else {
			int cur = pq.top();pq.pop();
			if(cur < R[i].t) pq.push(R[i].t + R[i].service);
			else {
				wait += cur - R[i].t;
				pq.push(cur + R[i].service);
			}
		}
	}
	printf("%.1f\n",wait/60.0/all);
	return 0 ;
}
发布了1106 篇原创文章 · 获赞 122 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/104186027