题目链接:http://poj.org/problem?id=2259

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

题意:

给出 $t$ 个团队,每个团队一行输入,给出正整数 $n$ 以及团队中 $n$ 个人各自的编号。

又给出一系列命令,分别是入队编号为 $x$ 的人和 出队编号为 $x$ 的人。

入队一个人,他会寻找队列中自己的队友,并排在他们的后面;若没有找到队友,则直接排在队伍最末尾。

出队一个人,则直接出队队首。要求给出每次出队的人的编号。

要求每次出入队都是常数时间复杂度。

题解:

用一种类似于邻接表的形式存储:开一个大队列,用于存储在队伍中的团队,再开若干小队列,用于存储每个团队内部的成员。

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxid=1e6+;
const int maxt=1e3+; int t;
int id2team[maxid];
queue<int> T;
queue<int> Q[maxt];
bool vis[maxt]; int main()
{
int kase=;
while(scanf("%d",&t) && t)
{
memset(id2team,,sizeof(id2team));
for(int team=,n;team<=t;team++)
{
scanf("%d",&n);
for(int i=,id;i<=n;i++)
{
scanf("%d",&id);
id2team[id]=team;
}
}
while(!T.empty()) T.pop();
for(int team=;team<=t;team++) while(!Q[team].empty()) Q[team].pop();
memset(vis,,sizeof(vis));
char op[];
printf("%sScenario #%d\n",kase>?"\n":"",++kase);
while(scanf("%s",op) && op[]!='S')
{
if(op[]=='E')
{
int id,team;
scanf("%d",&id);
team=id2team[id];
if(vis[team]) Q[team].push(id);
else Q[team].push(id), T.push(team), vis[team]=;
}
if(op[]=='D')
{
int team=T.front();
printf("%d\n",Q[team].front());
if(Q[team].size()>) Q[team].pop();
else Q[team].pop(), T.pop(), vis[team]=;
}
}
}
}

POJ 2259 - Team Queue - [队列的邻接表]的更多相关文章

  1. POJ 2259 Team Queue(队列)

    题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...

  2. (队列的应用5.3.2)POJ 2259 Team Queue(队列数组的使用)

    /* * POJ_2259.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  3. poj 2259 Team Queue

    Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2977   Accepted: 1092 Descri ...

  4. queue POJ 2259 Team Queue

    题目传送门 题意:先给出一些小组成员,然后开始排队.若前面的人中有相同小组的人的话,直接插队排在同小组的最后一个,否则只能排在最后面.现在有排队和出队的操作. 分析:这题关键是将队列按照组数分组,用另 ...

  5. UVA.540 Team Queue (队列)

    UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...

  6. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  7. Stack栈类与、Queue队列与线性表的区别和联系

    栈和队列都属于特殊的线性表   一.定义   1.线性表(linear list): 是数据结构的一种,一个线性表是n个具有相同特性的数据元素的有限序列.数据元素是一个抽象的符号,其具体含义在不同的情 ...

  8. 团体队列UVA540 Team Queue(队列简单用法)

    题目背景 队列和优先级队列是大多数计算机科学家都知道的数据结构.但是团队队列却不被人熟知,尽管在生活中经常出现.比如,午餐时间的食堂门口的队列就是一个团队队列.在一个团队队列中,每个元素属于一个团队. ...

  9. [POJ2259]Team Queue (队列,模拟)

    2559是栈,2259是队列,真的是巧啊 题意 模拟队列 思路 水题 代码 因为太水,不想打,发博客只是为了与2559照应,于是附上lyd的std #include <queue> #in ...

随机推荐

  1. mysql复制过程中的server-id的理解

    一.     server-id做什么用的,你知道吗? 1. mysql的同步的数据中是包含server-id的,用于标识该语句最初是从哪个server写入的,所以server-id一定要有的 2. ...

  2. 新手如何学习 jQuery?

    可以看张晓菲的<锋利的jQuery>,重点是自己理解函数用法并自行实现一些常用的效果.如果需要快速查阅可以用这个api,每个函数都附有简单的示例:http://api.jquery.com ...

  3. [Big Data - Kafka] Kafka设计解析(二):Kafka High Availability (上)

    Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永远不能再恢复,亦 ...

  4. 使用Fidder将生成环境代码映射到本地(文件夹)

    匹配生产URL正则表达式 regex:http://www.a.com/statics/js/(.*) 本地路径配置 D:\web\statics\js\$1

  5. java中的数据加密4 数字签名

    数字签名 它是确定交换消息的通信方身份的第一个级别.A通过使用公钥加密数据后发给B,B利用私钥解密就得到了需要的数据,问题来了,由于都是使用公钥加密,那么如何检验是A发过来的消息呢?上面也提到了一点, ...

  6. 说说自己对RESTful API的理解

    REST不是英文上的rest单词,其英文缩写为presentational State Transfer ,直译为表现状态转移,咋看起来很学术,不懂,其实不用去死抠这个词的意思.REST是一种约束和架 ...

  7. PHP最全笔记(一)(值得收藏,不时翻看一下)

    PHP笔记来啦~绝对干货! 以下为我以前学PHP时做的笔记,时不时的也会添加一些基础知识点进去,有时还翻出来查查. //语法错误(syntax error)在语法分析阶段,源代码并未被执行,故不会有任 ...

  8. Astah 使用 流程图、类图、时序图

    1 流程图         右键 _ create Diagrm _ add Flowchart _ New Flowchart      2 时序图         Create Diagram _ ...

  9. mysql 常用语句集

    1.查询某数据库大小语句: SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024),2),'MB') AS DATA  FROM TABLES WHERE tab ...

  10. [Bayes] openBUGS: this is not the annoying bugs in programming

    Bayesian inference Using Gibbs Sampling 允许用户指定复杂的多层模型,并可使用MCMC算法来估计模型中的未知参数. We use DAGs to specify ...