PAT 甲级 1017 Queueing at Bank (25分)优先队列

原题链接: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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值