题目链接: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. XPath轴(XPath Axes)总结

    XPath轴(XPath Axes)可定义某个相对于当前节点的节点集: 1.child 选取当前节点的所有子元素 2.parent 选取当前节点的父节点 3.descendant 选取当前节点的所有后 ...

  2. vue设置默认地址和配送方式

    1.截图 2.address.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  3. 【XMPP】XMPP协议之原理篇

    XMPP协议简介 XMPP协议(Extensible Messaging and Presence Protocol,可扩展消息处理现场协议)是一种基于XML的协议. 目的是为了解决及时通信标准而提出 ...

  4. 自己实现字符串转整数(不使用JDK的字符串转整数的方法)

    [需求]: (1)如果输入的字符串为null,为空,或者不是数字,抛出异常: (2)如果输入的数字超出int的最大值或最小值,抛出异常: (3)输入的数字允许以+或-号开头,但如果输入的字符串只有&q ...

  5. 【Java】Java NIO

    NIO 为什么要使用 NIO? NIO 的创建目的是为了让 Java 程序员可以实现高速 I/O 而无需编写自定义的本机代码.NIO 将最耗时的 I/O 操作(即填充和提取缓冲区)转移回操作系统,因而 ...

  6. java 实现websocket

    最近了解了下websocket和socket这个东西,说不得不来说下为何要使用 WebSocket ,和为何不用http. 为何需要WebSocket ? HTTP 协议是一种无状态的.无连接的.单向 ...

  7. 【iCore1S 双核心板_ARM】例程十五:USB_HID实验——双向数据传输

    实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore1S USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid ...

  8. Jquery计算指定日期加上多少天、加多少月、加多少年的日期

    /* * 功能:实现VBScript的DateAdd功能. * 参数:interval,字符串表达式,表示要添加的时间间隔. * 参数:number,数值表达式,表示要添加的时间间隔的个数. * 参数 ...

  9. Linux SD/MMC/SDIO驱动分析_转

    转自:Linux SD/MMC/SDIO驱动分析    https://www.cnblogs.com/cslunatic/p/3678045.html#3053341 一.SD/MMC/SDIO概念 ...

  10. Java知多少(53)使用Java创建自己的异常子类

    尽管Java的内置异常处理大多数常见错误,你也许希望建立你自己的异常类型来处理你所应用的特殊情况.这是非常简单的:只要定义Exception的一个子类就可以了(Exception当然是Throwabl ...