PAT(Advance)1014. Waiting in Line

PAT(Advance)1014. Waiting in Line

该题分制为30分。
题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936

题目要模拟银行的排号系统,这题要用队列来处理,而且我们总是去人少的地方排队,因为办理的人不知道前面需要花多久时间才办好,但是人数可以直观地看见。题目给出的Q个查询人,并不是按顺序给出的一开始我以为是顺序给出嘛,有两个测试点没过就猜到了。后来多用了一个数组解决了。题目还有一个坑就是,并不是结束时间超过5点就输出sorry,而是一开始服务的时间超过5点。所以当一个人16:59开始接受服务,到17:59结束也是可以的。

我和其他人的做法不太一样,代码量多一点。首先用sum数组记录所有人的完成时间。comp数组来记录队头的人结束服务的总时间,比较出最少的将队头出队,让下一个入队。可能有点难理解多看几遍应该能看懂。

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;
const int maxn = 1010;
const int INF = 9999999;
int N, M, K, Q;
int cust[maxn];
int t[maxn];
queue<int>q[22];
int sum[22] = { 0 };
int comp[22] = { 0 };
int c[maxn] = { 0 };

int MinIndex()
{
	int MIN = 99999999, index = 1;
	for (int i = 1; i <= N; i++)
	{
		int temp = q[i].front();
		if (comp[i] + temp < MIN)
		{
			MIN = comp[i] + temp;
			index = i;
		}
	}
	return index;
}

int main()
{
	cin >> N >> M >> K >> Q;
	for (int i = 1; i <= K; i++)
	{
		cin >> t[i];
	}
	for (int i = 1; i <= Q; i++)
	{
		cin >> cust[i];
		c[cust[i]] = -1;
	}
	int index = 1, j = 1;
	for (int i = 1; i <= K; i++)
	{
		int temp = 1;
		if (i <= N * M)
		{
			if (index > N)
			{
				index = 1;
			}
			q[index].push(t[i]);
			sum[index] += t[i];
		}
		else
		{
			temp = MinIndex();
			comp[temp] += q[temp].front();
			q[temp].pop();
			q[temp].push(t[i]);
			sum[temp] += t[i];
		}
		if (c[i]==-1)
		{
			int totalmin = 0,  preh = 8;
			if (i <= N * M)
			{
				totalmin = sum[index];
				preh += (sum[index] - t[i]) / 60;
			}
			else
			{
				totalmin = sum[temp];
				preh += (sum[temp] - t[i]) / 60;
			}
			if(preh<17)
			{
				c[i] = totalmin;
			}
			j++;
		}
		index++;
	}
	for (int i = 1; i <= Q; i++)
	{
		if (c[cust[i]] > 0)
			cout << setw(2) << setfill('0') << c[cust[i]] / 60 + 8 << ':' << setw(2) << setfill('0') << c[cust[i]] % 60 << endl;
		else
		{
			cout << "Sorry" << endl;
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值