UVA 540 —— Team Queue

原题:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=481

题意:有n组人,给出每组人的编号;

有两种操作 —— 1、ENQUEUE X 表示将编号为X的人入队,如果队列当中已经有与X同组的成员,那么就将X排在同组的最后一个成员的后面;如果队列中没有与X同组的成员,则将X排在整个队列的末尾;2、DEQUEUE 表示输出当前队首的编号并出队;

思路:开两个队列,一个用来储存每组在队的成员,另一个用来储存在队的组别(成员集合);


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1100;
queue<int>Q, q[maxn];
int id[10000000];
int n, cas = 0;
int main()
{
	while(~scanf("%d", &n))
	{
		if(n == 0)	break;
		printf("Scenario #%d\n", ++cas);
		while(!Q.empty())	Q.pop();
		for(int i = 1;i<=n;i++)
		{
			int m;
			while(!q[i].empty())	q[i].pop();
			scanf("%d", &m);
			for(int j = 1;j<=m;j++)
			{
				int x;
				scanf("%d", &x);
				id[x] = i;
			}
		}
		char cmd[10];
		scanf("%s", cmd);
		while(cmd[0] != 'S')
		{
			if(cmd[0] == 'E')
			{
				int k;
				scanf("%d", &k);
				int t = id[k];				
				if(q[t].empty())	
				Q.push(t);
				q[t].push(k);
			}
			else
			{					
				int top = Q.front();
				if(q[top].empty())	
				{
					Q.pop();
					top = Q.front();
				}				
				printf("%d\n", q[top].front());
				q[top].pop();
			}
			scanf("%s", cmd);
		}
		printf("\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值