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

解决方法

题目大意

有n个客户,k个窗⼝。已知每个客户的到达时间和需要的时⻓,如果有窗⼝就依次过去,
如果没有窗⼝就在⻩线外等候(⻩线外只有⼀个队伍,先来先服务),求客户的平均等待时⻓。银⾏
开放时间为8点到17点,再8点之前不开⻔,8点之前来的⼈都要等待,在17点后来的⼈不被服务。

第一次代码

思路

这道题属于模拟类型。
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
	int cometime, processtime;
}tmp;
vector<node>v;
bool cmp(node a, node b)
{
	return a.cometime < b.cometime;
}
int main()
{
	int n, k, hh, mm, ss, pt, cometime = 0;
	double totaltime = 0;
	scanf("%d %d", &n, &k);
	vector<int>endtime(k, 28800);//8点
	for (int i = 0; i < n; i++)
	{
		scanf("%d:%d:%d %d", &hh, &mm, &ss, &pt);
		cometime = hh * 3600 + mm * 60 + ss;
		tmp.cometime = cometime;
		tmp.processtime = pt*60;
		if (cometime <= 61200) v.push_back(tmp);
	}
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < v.size(); i++)
	{
		int index =0, minendtime = endtime[0];//不要顺手写成-1了
		for (int j = 1; j < k; j++)
		{
			if (endtime[j] < minendtime)
			{
				minendtime = endtime[j];
				index = j;
			}
		}
		if (endtime[index] <= v[i].cometime) endtime[index] = v[i].cometime + v[i].processtime;
		else
		{
			totaltime += (endtime[index] - v[i].cometime);
			endtime[index] += v[i].processtime;
		}
	}
	/*if (v.size() == 0) printf("0.0");
	else */printf("%.1f", totaltime / 60.0 / v.size());
	return 0;
}

第二次代码(更好)

思路

这种解法是在柳神的博客一看到的,昨晚就去看了一下关于优先队列的东西。
使用一个优先队列维护窗口办理完业务的时间~如果最早结束服务的窗口时间早于客户的到达时间,不需要等待,直接执行 q.push(v[i].come + v[i].time);否则客户的等待时间是q.top() - v[i].come~

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
	int cometime, processtime;
}tmp;
bool cmp(node a, node b)
{
	return a.cometime < b.cometime;
}
vector<node>v;
int main()
{
	int n, k, hh, mm, ss, pt,cometime=0;
	double total = 0;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++)
	{
		scanf("%d:%d:%d %d", &hh, &mm, &ss,&pt);
		cometime = hh * 3600 + mm * 60 + ss;
		tmp.cometime = cometime;
		tmp.processtime = pt * 60;
		if (cometime <= 61200) v.push_back(tmp);
	}
	sort(v.begin(), v.end(), cmp);
	priority_queue<int, vector<int>, greater<int>>q;
	for (int i = 0; i < k; i++) q.push(28800);
	for (int i = 0; i < v.size(); i++)
	{
		if (q.top() <= v[i].cometime)
		{
			q.push(v[i].cometime + v[i].processtime);
			q.pop();
		}
		else
		{
			total += q.top() - v[i].cometime;
			q.push(q.top() + v[i].processtime);
			q.pop();
		}
	}
	printf("%.1f", total / 60.0 / v.size());
	return 0;
}

后记

两种方法思路大体一样,但是方法我觉得第二种更胜一筹。更加容易理解,但是我第一次写,优先队列的使用不熟悉,需要多加熟悉。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值