题目链接: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. JS 全屏代码

    // 推断各种浏览器,找到正确的方法 function launchFullscreen(element) { if(element.requestFullscreen) { element.requ ...

  2. Fluent动网格【8】:网格节点运动案例

    Fluent动网格中的DEFINE_GRID_MOTION宏允许用户定义网格节点的运动.本案例演示采用DEFINE_GRID_MOTION宏指定边界节点的运动. 案例动网格效果如图所示. 案例描述 本 ...

  3. linux每日命令(28):chgrp命令

    在linux系统里,文件或目录的权限的掌控以拥有者及所属群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...

  4. 【Linux高级驱动】如何分析并移植网卡驱动

    dm9000的驱动分析 m9000_init platform_driver_register(); db); db); );  ; id_val ; id_val ; /* 获取芯片型号 */ id ...

  5. idea 自动根据屏幕代码换行

    勾上即可 -- 建议不开启 有些时候从其他来源网站直接copy的代码文件会出现不勾选还会出现自动换行的情况 解决方法  新建文件  代码全部选剪过去

  6. 模仿Semaphore自定义自己的 信号量

    简介 这里模仿Semaphore,自定义自己的信号量,利用AQS共享模式 1.MySemaphore.java package com.jacky; import java.util.concurre ...

  7. talk 1

    话转偏锋 让别人可以接话, 同时可以设计转换到的话题, 把"谈话带到正确的轨道", 就像下象棋一样, 要看三步 A: 很喜欢看篮球比赛, 对B说 我每次都堵湖人队会赢 B: 篮球最 ...

  8. 关于Unity中ARPG游戏人物移动(专题十一)

    ARPG:动作型角色扮演类游戏 大多数的ARPG游戏都是使用摇杆操作,以第三人称摄像机的方式来跟随主角,实际上人物只走八个方向,上,下,左,右,左上,左下,右下,右上 控制角色移动的思路1: 在ARP ...

  9. Windows平台使用RMAN命令自动删除Oracle过期归档日志的方法

    自动删除Oracle过期归档日志的思路如下: 1.编写自动执行的bat脚本文件: 2.Windows设置定期执行计划 OracleArchLogClear.bat D:/Oracle/product/ ...

  10. redis mongodb mysql 三大数据库的更简单的批量操作。批量任务自动聚合器。

    1.redis mongodb mysql的python包都提供了批量插入操作,但需要自己在外部对一个例如1000 001个任务进行分解成每1000个为1个小批次,还要处理整除批次数量后的余数,如果做 ...