POJ 2259 Team Queue【模拟队列】

本文介绍了一种特殊的数据结构——团队队列,并提供了一个高效的模拟实现示例。在该队列中,元素按所属团队聚集,并遵循特定的入队和出队规则。

Team Queue
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4449 Accepted: 1541

Description

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example. 

In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue. 

Your task is to write a program that simulates such a team queue.

Input

The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements. 
Finally, a list of commands follows. There are three different kinds of commands: 
  • ENQUEUE x - enter element x into the team queue 
  • DEQUEUE - process the first element and remove it from the queue 
  • STOP - end of test case

The input will be terminated by a value of 0 for t. 
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

Output

For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one. 

Sample Input

2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0

Sample Output

Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

int a[1011*1000];

int main()
{
	int T,Kase=0;
	while(~scanf("%d",&T),T) {
		for(int i=1;i<=T;++i) {
			int s; scanf("%d",&s);
			for(int j=1;j<=s;++j) {
				int tem=0; scanf("%d",&tem);
				a[tem]=i;
			} 
		}
		queue<int> Q[T+1];
		queue<int> qm;
		char str[22];  printf("Scenario #%d\n",++Kase); 
		while(~scanf("%s",str),str[0]!='S') {
			if(str[0]=='D') {
				int F=qm.front();
				int y=Q[F].front();  Q[F].pop();
				printf("%d\n",y); 
			    if(Q[F].empty()) qm.pop(); 
			}
			else {
				int x; scanf("%d",&x);
				if(Q[a[x]].empty()) qm.push(a[x]);
				Q[a[x]].push(x);
			}
		}
		puts("");
	}
	return 0;
} 


POJ2259—团队队列问题 时间限制:2000ms,空间限制:65536K 问题描述:队列和优先级队列是大多数计算机科学家都知道的数据结构。然而团队队列Team Queue)并不那么出名,尽管它经常出现在日常生活中。在团队队列中,每个队员都属于一个团队,如果一个队员进入队列,它首先从头到尾搜索队列,以检查其队友(同一团队的队员)是否已经在队列中,如果在,他就会进到该队列的后面,如果不在,他进入当前尾部的队列并成为该队列的一个队员(运气不好)。像在普通队列中一样,队员按照他们在队列中出现的顺序从头到尾进行处理。你的任务是编写一个模拟此团队队列的程序。 输入格式:输入将包含一个或多个测试用例。每个测试用例以团队数量 t(1≤t≤1000)开始,然后是团队描述,每个团队描述由该团队的队员数和队员本身组成,每个队员是一个 0 到 999999 范围内的整数,一个团队最多可包含 1000 个队员,最后是一个命令列表,有以下三种不同的命令: (1)ENQUEUE x:将队员 x 进入团队队列。 (2)DEQUEUE:处理第一个元素并将其从队列中删除。 (3)STOP:测试用例结束。 输入 t 为 0 时终止。警告:测试用例最多可包含 200000(二十万)个命令,因此团队队列的实现应该是高效的:队员进队和出队应该只占常量时间。 输出格式:对于每个测试用例,首先输出一行"Scenario #k",其中 k 是测试用例的编号,然后对于每个 DEQUEUE 命令,输出一行指出出队的队员。在每个测试用例后输出一个空行,包括最后一个测试用例之后。 输入样例: 2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202 ENQUEUE 103 ENQUEUE 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 ENQUEUE 259001 ENQUEUE 260001 ENQUEUE 259002 ENQUEUE 259003 ENQUEUE 259004 ENQUEUE 259005 DEQUEUE DEQUEUE ENQUEUE 260002 ENQUEUE 260003 DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 0 输出样例: Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001 要求:①提供详细的算法设计思路,思路分为输入、处理和输出三个部分。②生成解题的Java代码。允许存储所有输入,并一次性输出结果,解题代码需包含丰富的中文注释。③解题代码和算法设计思路分开显示,并使用文本格式而非Markdown格式显示。
最新发布
01-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值