hdu1387 模拟队列
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 |
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 |
Sample Output
Scenario #1 |
题意:输入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 模拟队列的更多相关文章
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
- 5. redis管道, 发布订阅, 模拟队列
一. 发布订阅 #订阅scribe 127.0.0.1:6379> SUBSCRIBE "channel_1" Reading messages... (press Ctrl ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈
LinkedList的特有方法: (一)添加方法 addFisrt(E e):将指定元素插入此列表的开头.//参数e可以理解成Object对象,因为列表可以接收任何类型的对象,所以e就是Object对 ...
- 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...
- Splay-Tree总结一:模拟队列
伸展树是一种强大的数据结构,由于其特性,可以很好地模拟队列的插队等操作,而线段树解决这类问题通常需要转化一下,比较伤脑筋 而用伸展树的解决方法就是先建好一颗节点数等于队列长度的树,每个队列元素在队列中 ...
- PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)
源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...
- php中数组模拟队列、栈的函数以及数组指针操作
1,数组指针,current表示当前指针,输出其指向的元素:next表示指针移动到下一个元素:prev指针移动到上一个元素:end表示指针移动到最后一个元素:reset表示指针移动到第一个元素: &l ...
- 2018.9.5 Java中使用栈来模拟队列
栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...
随机推荐
- DHCP工作工程
1.客户端请求IP 客户端发一个DHCP DISCOVEY(包含主机名.mac地址)广播包 2.服务端响应请求 DHCP服务器收到请求后,查看自己的地址池是否有合法的地址.如果有,广播一个DHCP o ...
- SQL基础E-R图画法
例一.假设有以下表:T1(a1,a2, a3, a5)T2(a3,a4)T3(a5, a6)T4(a3, a5, a7)其中带下划线的属性标识为所在关系模式的主码T1中的a3是参照T2的外码T1中的a ...
- sql基本查询语句练习
student(S#,Sname,Sage,Ssex) 学生表 S#:学号: Sname:学生姓名:Sage:学生年龄:Ssex:学生性别 Course(C#,Cname,T#) 课程表 ...
- structs2----数据封装以及拦截器
技术分析之在Struts2框架中使用Servlet的API 1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet ...
- 关联映射、关联查询【重点掌握一条SQL语句的那种方法】
1 什么叫关联映射 通过数据库对象之间的关联关系(一对一.一对多.多对多),反映到实体对象上之间的引用. 举例 用户实体类(User):user_id user_name user_token 笔记本 ...
- BuilderPattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- hive与hbase数据类型对应关系
hive与hbase数据类型对应关系 当hbase中double,int 类型以byte方式存储时,用字符串取出来必然是乱码. 在hivd与hbase整合时也遇到这个问题:做法是:#b 1.加#b C ...
- 6.Model类
Basic Concepts 在Model/View结构中,Model提供标准的接口让View和Delegate获得数据.在QT中,标准的接口都被定义在QAbstractItemModel类 ...
- SDUT 3379 数据结构实验之查找七:线性之哈希表
数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...
- 关于解决cmd中执行java提示"找不到或无法加载主类"的问题
昨天学生遇到一个问题:在cmd命令行中,用javac编译java文件可以成功,但是用java执行却提示“找不到或无法加载主类”.现将该问题的原因以及解决办法记录一下. 先理解一下系统变量path和cl ...