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. Python 核心数据类型

    1.Python中一切皆对象 2.Python中不需要申明对象类型,对象的类型由运行的表达式决定 3.创建了对象意味着绑定了对象的操作到此对象,也就是在固有的对象上只能调用该对象特有的操作.比如只能将 ...

  2. Ubuntu 软件包管理详解

    原文转载自:http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html Ubuntu 方便宜用,最值得让人称道的便是其安装软件的方式, 一条 ...

  3. 机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA)

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  4. 简易版CSS3 Tab菜单 实用的Tab切换

    今天我们要来分享一款非常简易而又实用的CSS3 Tab菜单,Tab菜单没有非常华丽的动画,但是代码非常简洁易懂,也可以在大部分场合使用,因此也非常实用,如果你需要加入动画效果,也可以自己方便地修改这款 ...

  5. maven 的各种命令

    mvn clean : 清理旧的文件 mvn clean compile :  清理 .编译 mvn clean test :       清理 .编译 .测试 mvn clean package : ...

  6. homework-09

    这次作业主要考察C++11的简单用法,个人感觉这样的练习对我这种编程能力比较差的非常有用,加深了对C++11的理解. Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. ...

  7. jQuery基础学习7——层次选择器find()方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 开发程序过程中遇到的调用Web Api小问题

    在用Umbraco Web Api开发程序时,前端使用React调用Web Api 当时是有一个页面Search.cshtml,把用React产生的脚本代码,在这个页面进行引用 写了一个Api, 调用 ...

  9. 使用friso中文分词注意

    friso是使用c语言开发的一款中文分词器,使用流行的mmseg算法实现.完全基于模块化设计和实现,可以很方便的植入到其他程序中,例如:MySQL,PHP等.目前只支持UTF-8 编码. 源码无需修改 ...

  10. CodeForces 705C Thor (模拟+STL)

    题意:给定三个操作,1,是x应用产生一个通知,2,是把所有x的通知读完,3,是把前x个通知读完,问你每次操作后未读的通知. 析:这个题数据有点大,但可以用STL中的队列和set来模拟这个过程用q来标记 ...