Uva - 540 - Team Queue



用两个队列,每个团队有一个队列,包含自己队伍中的人员编号,而团队整体形成一个队列,包含队列中的团队编号。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;

const int maxt = 1010;

int main()
{
	int t, kase = 0;
	while (scanf("%d", &t) == 1 && t) {
		printf("Scenario #%d\n", ++kase);

		map<int, int> team;
		// team[x]表示编号为x的人所在的团队编号
		for (int i = 0; i < t; i++) {
			int n, x;
			scanf("%d", &n);
			while (n--) {
				scanf("%d", &x);
				team[x] = i;
			}
		}
		queue<int> qt, qp[maxt];
		// qt是团队的队列,qp[i]是团队i成员队列
		while (1) {
			char cmd[10];
			scanf("%s", cmd);
			if (cmd[0] == 'S') {
				break;
			}
			else if (cmd[0] == 'E') {
				int x;
				scanf("%d", &x);
				int t = team[x];
				if (qp[t].empty()) {
					// 如果x所在的团队没有人在团队队列中,团队t入列
					qt.push(t);
				}
				qp[t].push(x);
			}
			else {
				int t = qt.front();
				printf("%d\n", qp[t].front());
				qp[t].pop();
				if (qp[t].empty()) {
					qt.pop();
					// 如果某个人出队后,队伍空了
					// 那就把队伍从队伍队列中出列
				}
			}
		}
		printf("\n");
	}

	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值