原题链接:1017 Queueing at Bank (25分)
题目大意:
有n个客户,m个窗口。已知每个客户的到达时间和服务时长,如果有窗口空着,那么可以依次过去办理,没有窗口空着,人们就在黄线外等候(挂号且排队),按照顺序走向前办理业务。
分析:
用结构体存客户的到达时间,通过格式化读入,后续分析请看注释
满分代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int MAXN = 1e4+10, M = 110;
/*
题目大意:有n个客户,m个窗口。已知每个客户的到达时间和服务时长,如果有窗口空着,那么
可以依次过去办理,没有窗口空着,人们就在黄线外等候(挂号且排队),按照顺序走向前办理
业务。
分析:用结构体存客户的到达时间,通过格式化读入,后续分析看注释
*/
int n, m;
struct Person {
int arrive_time; // 从0时刻开始的秒数
int service_time; // 用秒存
// 重载 按照到达时间排序
bool operator< (const Person& b) {
return arrive_time < b.arrive_time;
}
}persons[MAXN];
int main() {
cin >> n >> m;
for(int i = 0; i < n; i++) {
int hour, minute, second, service_time;
scanf("%d:%d:%d %d", &hour, &minute, &second, &service_time);
// 服务时间不能超过60min,所以与60取max
service_time = min(service_time, 60);
// 到达时间arrive_time也按照0点开始的秒数计
persons[i] = {hour*3600 + minute*60 + second, service_time * 60};
// cout << hour*3600 + minute*60 + second << " " << service_time*60 << endl;
}
// 优先队列小根堆存所有窗口的上一个服务结束时间
priority_queue<int, vector<int>, greater<int> > windows;
// m个窗口排好
for(int i = 0; i < m; i++) windows.push(8*60*60);
// 定义了服务总时长和服务人数
int sum = 0, cnt = 0;
sort(persons, persons + n);
for(int i = 0; i < n; i++) {
auto person = persons[i];
// 用w代表当前最早结束时间的窗口
int w = windows.top();
windows.pop();
// 先剔除到达时间超过5点的
if(person.arrive_time > 17 * 3600) break;
// 开始服务时间
int start_time = max(w, person.arrive_time);
sum += start_time - person.arrive_time;
cnt++;
windows.push(start_time + person.service_time);
// cout << person.arrive_time << " " << w << " " << start_time << " " << start_time+person.service_time << endl;
}
printf("%.1lf\n", (double)sum / cnt / 60);
return 0;
}