uvaoj 540 Team Queue 队列模拟

20 篇文章 0 订阅
uvaoj 540 Team Queue 队列模拟
t个团队的人在排队,每新来一个人时,如果它有队友在排队,那么这个人就排在最后一个队友的后边,如果没有任何队友排队,那么这个人排到长队的队尾,我们可以将这个队列看做两级,一级是团队队列,一个是团队自身的队列。模拟一下就行了。
代码如下:
/*************************************************************************
	> File Name: 540.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2015年01月21日 星期三 15时48分29秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 1010

int main(int argc, char *argv[])
{
	int n;
	int c = 0;
	while (scanf("%d", &n) != EOF) {
		if (n == 0) {
			break;
		}
		// 记录所有人的团队编号
		map<int, int> team;
		for (int i = 0; i < n; ++i) {
			int x, y;
			scanf("%d", &x);
			while (x--) {
				scanf("%d", &y);
				team[y] = i;
			}
		}
		queue<int> qteam;		// 团体队列
		queue<int> q[N];			// q[i]是团队i成员的队列
		printf("Scenario #%d\n", ++c);
		while (1) {
			char cmd[10];
			scanf("%s", cmd);
			if (cmd[0] == 'S') {
				break;
			} else if (cmd[0] == 'D') {
				int t = qteam.front();
				printf("%d\n", q[t].front());
				q[t].pop();
				if (q[t].empty()) {
					qteam.pop();	// 团队t全体队员出队
				}
			} else {
				int x;
				scanf("%d", &x);
				int t = team[x];
				if (q[t].empty()) {
					qteam.push(t);	// 团队t进入队列
				}
				q[t].push(x);
			}
		}
		printf("\n");
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值