PAT (Advanced) 1017. Queueing at Bank (25)

本文介绍了一个计算多个服务窗口中顾客平均等待时间的程序。通过结构体存储顾客到达时间和所需服务时间,并采用排序算法来优化顾客服务顺序,最终计算出所有有效顾客的平均等待时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Customer
{
	int arrive_time, cost_time;
};

bool cmp(const Customer &a, const Customer &b)
{
	return a.arrive_time < b.arrive_time;
}

int char2int(char c[])
{
	int hh = (c[0] - '0') * 10 + c[1] - '0';
	int mm = (c[3] - '0') * 10 + c[4] - '0';
	int ss = (c[6] - '0') * 10 + c[7] - '0';
	return hh * 3600 + mm * 60 + ss;
}

int main()
{
	int n, k;
	cin >> n >> k;
	vector<int> end(k, 8 * 3600);
	vector<Customer> vc;
	char arr[10];
	int arrive, time;
	Customer temp;
	for (int i = 0; i < n; i++)
	{
		/*scanf("%s, %d", arr, &time);*/
		cin >> arr >> time;
		arrive = char2int(arr);
		if (arrive <= 17 * 3600)
		{
			temp.arrive_time = arrive;
			temp.cost_time = time * 60;
			vc.push_back(temp);
		}
	}
	sort(vc.begin(), vc.end(), cmp);
	
	int wait_time, total_wait = 0;
	int early;
	int index;
	bool nowait;
	for (auto it = vc.begin(); it != vc.end(); ++it)
	{
		wait_time = 0;
		early = 0x7fffffff;
		nowait = false;
		for (int i = 0; i < k; i++)
		{
			if (end[i] <= (*it).arrive_time)
			{
				end[i] = (*it).arrive_time + (*it).cost_time;
				nowait = true;
				break;
			}
			else
			{
				if (end[i] < early)
				{
					early = end[i];
					index = i;
				}
			}
		}
		if (!nowait)
		{
			wait_time = end[index] - (*it).arrive_time;
			end[index] = end[index] + (*it).cost_time;
		}
		total_wait += wait_time;
	}
	printf("%.1lf\n", total_wait / vc.size() / 60.0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值