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. group()、start()、end()、span()

  2. sqlplus 连接数据库执行SP

    .bashrc export HOME= export LANG="C" .bash_profile #明码 #export LOG_USER=lhcx #export LOG_P ...

  3. Winform状态栏控件中Label靠右显示的方法

    设计器:   代码: 在Form_Load事件中添加 : statusStripMain.LayoutStyle= ToolStripLayoutStyle.HorizontalStackWithOv ...

  4. Android getWidth和getMeasuredWidth的区别

    getWidth 得到的事某个View的实际尺寸. getMeasuredWidth 得到的是某个View想要在parent view里面占的大小 相比你也见过这样的解释,听起来这样的解释也是云里雾里 ...

  5. day17 9.关闭资源与异常处理

    Java程序跟任何外部设备进行连接之后,都要把连接断开,把资源释放掉.Connection是一个重量级资源,Connecton占内存,Connection的获取是比较消耗资源和内存的.finally是 ...

  6. 外部访问docker内部容器centos的http服务

    1.创建容器 docker run -d -it -h dd -p --name bbbbb centos dd 是用户名 --name 后面是容器名字 2.在我们开始安装Nginx及其他所需软件之前 ...

  7. 设置 PyCharm 软件中 Terminal 窗口 中启动的 python 交互命令的版本

    设置 PyCharm 软件中 Terminal 窗口 中启动的 python 交互命令的版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 ...

  8. Boost 线程学习笔记

    Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...

  9. Halcon从某一个图片以指定区域绘制到另一个图像

    ************************************************************* * Halcon从某一个图片以指定区域绘制到另一个图像 * Author: ...

  10. 适合新手的Python爬虫小程序

    介绍:此程序是使用python做的一个爬虫小程序  爬取了python百度百科中的部分内容,因为这个demo是根据网站中的静态结构爬取的,所以如果百度百科词条的html结构发生变化 需要修改部分内容. ...