Team Queue
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2471   Accepted: 926

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
 #include"iostream"
#include"map"
#include"queue"
#include"cstring"
#include"string"
#include"cstdio"
using namespace std;
//map<int,int> m;
int m[];
queue<int> q;//队伍序号
queue<int> qq[];//入队
int flag[];
int ncase=;
int t,n,ele;
void inti();
void input();
void solve();
int main()
{
while(scanf("%d",&t)==&&t)
{
inti();
input();
solve();
}
return ;
}
void input()
{
for(int i=;i<t;i++)
{
//cin>>n;
scanf("%d",&n);
for(int j=;j<n;j++)
{
//cin>>ele;
scanf("%d",&ele);
m[ele]=i;
}
}
}
void inti()
{
for(int i=;i<t;i++)
{
flag[i]=;
while(!qq[i].empty())
qq[i].pop();
}
while(!q.empty())
q.pop();
}
void solve()
{
string com;
int a;
//cout<<"Scenario #"<<++ncase<<endl;
printf("Scenario #%d\n",++ncase);
while(cin>>com,com!="STOP")
{
if(com=="ENQUEUE")
{
//cin>>a;
scanf("%d",&a);
if(!flag[m[a]])
{
flag[m[a]]=;
q.push(m[a]);
}
qq[m[a]].push(a);
}
else if(com=="DEQUEUE")
{
int order=q.front();
cout<<qq[order].front()<<endl;
qq[order].pop();
if(qq[order].empty())
{
q.pop();
flag[order]=;
}
}
}
cout<<endl;
}

Team Queue的更多相关文章

  1. hdu 1387(Team Queue) STL

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Team Queue (uva540 队列模拟)

    Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...

  3. ACM题目————Team Queue

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  4. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. Team Queue(多队列技巧处理)

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. Team Queue(STL练习题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others ...

  7. Team Queue (HDU:1387)

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  8. Team Queue POJ - 2259 (队列)

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  9. POJ 2259 - Team Queue - [队列的邻接表]

    题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...

随机推荐

  1. 链表回文串判断&amp;&amp;链式A+B

    有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...

  2. Language Basics:语言基础

    Java包含多种变量类型:Instance Variables (Non-Static Fields)(实例变量):是每个对象特有的,可以用来区分各个实例Class Variables (Static ...

  3. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇:简介及目录》(附上完整工程文件)

    G-3D引擎2D射击类游戏制作教程 游戏类型: 打飞机游戏属于射击类游戏中的一种,可以划分为卷轴射击类游戏. 视觉表现类型为:2D 框架简介: Genesis-3D引擎不仅为开发者提供一个3D游戏制作 ...

  4. jackson 注解的使用

    在实体对象上添加 @JsonAutoDetect , 表明对该实体对象序列化成json串. @JsonAutoDetect public class User{ private int id; pri ...

  5. PHP操作cookie函数:setcookie()与setrawcookie()

    PHP setcookie() 函数向客户端发送一个 HTTP cookie.cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过浏览器 ...

  6. HDU 5742 It's All In The Mind (贪心)

    It's All In The Mind 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...

  7. 新浪云sae 邮件服务 quicksend()

    <?php header("Content-Type: text/html;charset=utf-8"); $mail = new SaeMail(); $form_Con ...

  8. MySQL安装配置最后时未响应解决方法

    安装MySQL出示未响应,一般显示在安装MySQL程序最后一步的2,3项就不动了. 这种情况一般是你以前安装过MySQL数据库服务项被占用了.解决方法:一种方法:你可以安装MySQL的时候在这一步时它 ...

  9. js获取服务端IP及端口及协议

    alert("location:"+window.location); alert("href: "+window.location.href); alert( ...

  10. 转载Agile Development 敏捷软件开发介绍

    转载原地址: http://blog.csdn.net/wayne_ran/article/details/1601008 敏捷开发(agile development)是一种以人为核心.迭代.循序渐 ...