例题5-6 团体队列(Team Queue,UVa 540)

原题链接:https://vjudge.net/problem/UVA-540
分类:<queue>
备注:queue与STL其他容器的综合运用

思路

  看题目标题一般都会考虑队列的,题目内容确实也是排队。
  已知队伍数目上限1000,那么开数组把不同队伍分开是可以接受的。
  用queue数组存取在排队的人,每次新来一个人找已用过的queue中是否有队友,有则加入队伍,无则再用一个queue存入新来的。这样每个队伍已经根据数组的下标排好了顺序,每当前面队伍的人全走完了才会找下一个队伍的人,即可符合题目要求。

代码如下:

#include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int maxt = 1000 + 5;
int t, n, x, kase;

int main(void)
{
	while (~scanf("%d", &t) && t)
	{
		printf("Scenario #%d\n", ++kase);
		map<int, int>team;
		for(int i=0;i<t;i++)
		{
			scanf("%d", &n);
			for (int j = 0; j < n; j++)
			{
				scanf("%d", &x);
				team[x] = i;	
			}
		}
		string cmd;
		queue<int>wait[maxt];
		int now = 0, cnt = 0;
		while (cin >> cmd)
		{
			if (cmd[0] == 'S')break;
			else if (cmd[0] == 'E')
			{
				scanf("%d", &x);
				int ok = 0;
				for (int i = now; i < cnt; i++)
					if (!wait[i].empty() && team[wait[i].front()] == team[x])
					{
						wait[i].push(x);
						ok = 1;
						break;
					}
				if (!ok)wait[cnt++].push(x);
			}
			else if (cmd[0] == 'D')
			{
				if (wait[now].empty()) now++;
				printf("%d\n", wait[now].front());
				wait[now].pop();
			}
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值