Team Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 129 Accepted Submission(s): 63
 
Problem 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.

 
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
 

题意:输入n个队列(q[i])(要对每个队列进行编号),每个队列有m个元素。接下来有三种操作。自己建立一个新的总队列Q(队列的队列)

1.

ENQUEUE,向队列Q压入一个元素a,判断这个元素a是不是属于输入队列q[i]中的元素,是的话,就压入q[i]中去

2.

DEQUEUE,处理Q队列中的第一个分队列,输出每个分队列的队首元素,输出之后将该分队列的队首元素pop出去,如果该分队列为空,就将该分队列从总队列中除名

3.

STOP,停止输入。输入0表示输入结束,程序终止

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<map>
using namespace std;
int main()
{
int n,k=;
int vis[];
while(scanf("%d",&n)!=EOF&&n)
{
memset(vis,,sizeof(vis));
queue<int>q[];//分队列
queue<int>Q;//总队列
map<int,int>team;
for(int i=;i<=n;i++)
{
int m;
scanf("%d",&m);
for(int j=;j<m;j++)
{
int member;
cin>>member;
team[member]=i;//为每个队员与团队编号建立关系
}
}
printf("Scenario #%d\n",k++);
string str;
while(cin>>str)
{
if(str=="STOP")
break;
else if(str=="ENQUEUE")
{
int mm;
cin>>mm;
q[team[mm]].push(mm);//将该元素插到对应分队列中
if(vis[team[mm]]==)//如果该分队列还没有进入总队列
{
Q.push(team[mm]);
vis[team[mm]]=; }
} else if(str=="DEQUEUE")
{
printf("%d\n",q[Q.front()].front());//输出总队列中处于队首的分队列的队首元素
q[Q.front()].pop();
if(q[Q.front()].empty())//如果该分队列为空
{
vis[Q.front()]=;
Q.pop();//将该分队列除名
} }
}
printf("\n");
} return ;
}


hdu1387 模拟队列的更多相关文章

  1. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  2. 5. redis管道, 发布订阅, 模拟队列

    一. 发布订阅 #订阅scribe 127.0.0.1:6379> SUBSCRIBE "channel_1" Reading messages... (press Ctrl ...

  3. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  4. Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈

    LinkedList的特有方法: (一)添加方法 addFisrt(E e):将指定元素插入此列表的开头.//参数e可以理解成Object对象,因为列表可以接收任何类型的对象,所以e就是Object对 ...

  5. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...

  6. Splay-Tree总结一:模拟队列

    伸展树是一种强大的数据结构,由于其特性,可以很好地模拟队列的插队等操作,而线段树解决这类问题通常需要转化一下,比较伤脑筋 而用伸展树的解决方法就是先建好一颗节点数等于队列长度的树,每个队列元素在队列中 ...

  7. PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)

    源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...

  8. php中数组模拟队列、栈的函数以及数组指针操作

    1,数组指针,current表示当前指针,输出其指向的元素:next表示指针移动到下一个元素:prev指针移动到上一个元素:end表示指针移动到最后一个元素:reset表示指针移动到第一个元素: &l ...

  9. 2018.9.5 Java中使用栈来模拟队列

    栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...

随机推荐

  1. 【259】ucpole.dat update

    2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...

  2. 最短路dijkstra堆优化

    demo: #include<bits/stdc++.h> #define max_v 102000 #define inf 0x3f3f3f3f using namespace std; ...

  3. Swing简介

    ---------------siwuxie095                         Swing 简介:     Java Swing 是 Java Foundation Classes ...

  4. winform 对话框控件

    ColorDialog 可以调节颜色的控件,如果给一个按钮点击事件 ColorDialog.showdialog();就会弹出这个 返回值是个枚举类 然后定义一个这个类的变量 接收一下它的返回值 Di ...

  5. WINFORM 开发模式,窗体回到默认样式方法。

    软件分为3类   客户端  网站应用  app WINFORM 主要用来只做客户端应用程序.C/S 客户端程序很重要的特点:可以操作用户电脑上的文件,执行在客户端上,电脑的配置越高执行就越流畅. 在p ...

  6. 在Oracle 12C中使用scott账号

    在Oracle11g中默认是有scott账号的,但在Oracle 12C中则不能直接使用. 我的机器环境: 操作系统:Windows Server 2008 R2 64位 Oracle版本:Oracl ...

  7. Linux 控制台/终端/tty/shell

    一.简介 使用linux已经有一段时间,却一直弄不明白这几个概念之间的区别.这些概念本身有着非常浓厚的历史气息,随着时代的发展,他们的含义也在发生改变,它们有些已经失去了最初的含义,但是它们的名字却被 ...

  8. Luogu 3704 [SDOI2017]数字表格

    列一下式子: $\prod_{i = 1}^{n}\prod_{j = 1}^{m}fib_{gcd(i, j)}$ 很套路的变成这样: $\prod_{d = 1}^{min(n, m)}fib_{ ...

  9. post请求时,出现relationship name error

    原因:在post请求,其属性名在代码中已经修改为partyLocationDtos,但是在swagger中,仍然为partyLocations. 因此,报错.记住,在post请求的时候,要一一对应.写 ...

  10. GDB中应该知道的几个调试方法【转陈浩】

    七.八年前写过一篇<用GDB调试程序>, 于是,从那以后,很多朋友在MSN上以及给我发邮件询问我关于GDB的问题,一直到今天,还有人在问GDB的相关问题.这么多年来,有一些问题是大家反复 ...