PAT 甲级 1017 Queueing at Bank
n个人,k个窗口,要计算所有人的平均等待时长,我们让优先级队列记录每个人的结束时间,按最小堆排序,这样每次出队列的都是最早结束的。插入节点前判断一下,如果当前队首时间小于插入时间,说明该窗口空闲,插入即可。否则说明需要等待,记录等待时间q.top() - v[i].come
// 1017 Queueing at Bank.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define maxN 10005
using namespace std;
struct person {
int come, time;
};
bool cmp(person p1, person p2) {
return p1.come < p2.come;
}
person v[maxN];
int main()
{
int n, k,cnt = 0, total = 0;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int hour, second, minute, time;
scanf("%d:%d:%d %d", &hour, &minute, &second, &time);
int temp = hour * 60 * 60 + minute * 60 + second;
if (temp > 61200) continue;
v[++cnt].come = temp;
v[cnt].time = time * 60;
}
sort(v + 1, v + 1 + cnt, cmp);
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 1; i <= k; i++) q.push(28800);
for (int i = 1; i <= cnt; i++) {
if (q.top() <= v[i].come) {
q.push(v[i].come + v[i].time);
q.pop();
}
else {
total += q.top() - v[i].come;
q.push(q.top() + v[i].time);
q.pop();
}
}
(cnt == 0)? printf("0.0") :printf("%.1f", ((double)total / 60.0 / (double)cnt));
return 0;
}
优先级队列 第一个参数为保存的数据类型,第二个参数为一个容器,一般为vector,不可为list
第三个参数默认为less< int> ,该优先级队列实现最大堆,队首的是数据最大的,降序排序
greater< int>代表最小堆,队首数据最小,升序排序