Team Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2051    Accepted Submission(s): 713

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

 
Source
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1103 1341 1308 1072 1702 
 
排队问题,一个term为一个单位,前面如果有你的term你可以直接进term的队尾,没有你就在整个队最后,自己新建自己的term并排在队尾。相当于双重队列
开始直接用两个队列模拟过程 TLE 了,换一个做法,有两个操作:
1、进队:
  用map匹配先队列中有没有你的term,有的话在你排在 term最后,没有的话新建term,并排在整体最后;
2、出队
  直接取整体第一个term,并取出第一个人,取完后判断term是否为空,为空解散;
 
 //608MS    4544K    1051B    G++
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int N = ;
typedef queue<int> INTQ; int main()
{
int n,m,a;
char op[];
int cas=;
while(cin>>n && n){
map<int ,int>TERM;
for(int i=;i<=n;i++){
cin>>m;
for(int j=;j<m;j++){
cin>>a;
TERM[a]=i;
}
} INTQ V[N];
map<int, int>M2V;
int index=;
int head_index=index;
cout<<"Scenario #"<<cas++<<endl;
while(cin>>op){
if(strcmp(op, "STOP")==){
break;
}
else if(strcmp(op, "ENQUEUE")==){
cin>>a;
if(M2V[TERM[a]]==){
INTQ tQ;
tQ.push(a);
V[index] = tQ;
M2V[TERM[a]] = index;
index = (index+)%N;
}else{
V[M2V[TERM[a]]].push(a);
} }else{
int a=V[head_index].front();
V[head_index].pop();
cout<<a<<endl;
if(V[head_index].empty()){
M2V[TERM[a]] = ;
head_index = (head_index+)%N;
}
} }
cout<<endl;
}
return ;
}

hdu 1387(Team Queue) STL的更多相关文章

  1. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

  2. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  3. c/c++ linux 进程间通信系列6,使用消息队列(message queue)

    linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...

  4. 消息队列(Message Queue)简介及其使用

    消息队列(Message Queue)简介及其使用 摘要:利用 MSMQ(Microsoft Message Queue),应用程序开发人员可以通过发送和接收消息方便地与应用程序进行快速可靠的通信.消 ...

  5. Java分布式:消息队列(Message Queue)

    Java分布式:消息队列(Message Queue) 引入消息队列 消息,是服务间通信的一种数据单位,消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.队列,是一种常见的数据结 ...

  6. 优先队列——二项队列(binominal queue)

    [0]README 0.1) 本文文字描述部分转自 数据结构与算法分析, 旨在理解 优先队列——二项队列(binominal queue) 的基础知识: 0.2) 本文核心的剖析思路均为原创(inse ...

  7. Linux等待队列(Wait Queue)

    1. Linux等待队列概述 Linux内核的等待队列(Wait Queue)是重要的数据结构,与进程调度机制紧密相关联,可以用来同步对系统资源的访问.异步事件通知.跨进程通信等.在Linux中,等待 ...

  8. 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))

    扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...

  9. HDU 5510---Bazinga(指针模拟)

    题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...

随机推荐

  1. Java 2D API - 2. Graphics 入门

    Java 2D API强大而复杂,不过大多时候我们只需使用java.awt.Graphcis类的部分功能.下面的内容将覆盖大多数的常见应用. Graphics 类中的方法大致可以分为两类: Draw ...

  2. C#TreeView控件遍历文件夹下所有子文件夹以及文件

    一直对递归的理解不深刻,有时候觉得很简单,可是用起来总会出错.这里需要在TreeView控件里显示一个文件夹下的所有目录以及文件,毫无意外的需要用到递归. 一开始,想到用递归写一个生成每一个节点(Tr ...

  3. java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~

    在工作开发过程中,每次用SVN提交代码全选择的时候,发现会产生很多不需要的文件后缀垃圾文件,感觉挺烦人的,一个一个删太麻烦了,如果产生多种后缀文件时,那删起来多费劲,是吧?想想,就写了一段程序通过递归 ...

  4. 解决Debian系统的Crontab执行时间时差问题

    首先用 * * * * * date >> /root/log.log 做个测试,发现显示的是UTC的时间,但是直接执行date,得到的是CST的时间.可见在Debian里crontab的 ...

  5. 用EXCEL内嵌的Visual Basic for Application 编程,通过 UGSimple USB-GPIB 控制器来驱动仪器34401A,并从34401A读取数据

    现在市场上有很多中USB-GPIB 控制器,或叫 USB 转GPIB链接线. 每种GPIB控制器都有它的 函数库(dll库).各种GPIB 控制器的价钱插别很大.这里以一种价钱较便宜的USB-GPIB ...

  6. ubuntu中搭建php7+mongodb方法

    首先照着这篇文章操作 http://blog.csdn.net/Toshiya14/article/details/51417076 结果发现一直报Cannot find OpenSSL's libr ...

  7. zxing--条码图像处理库

    ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码.   该项目可实现的条形码 ...

  8. javascript知识点总结----Function定义

    ---恢复内容开始--- 函数Function: 函数实际上是对象,每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法,函数名实际上是一个指向函数的指针,不会与某个函数绑定 ...

  9. private + virtual in Java/C++

    在Java中,private方法是隐式final的,就是说即使在子类中定义一个一模一样的方法,编译器认为这是两个没有联系的方法.private方法不参与运行时多态,这点和 final方法.static ...

  10. JS中this的指向问题&使用call或apply模拟new

    this的指向由调用时决定而不是定义时决定,定义的方式: //直接定义在函数里 var a="window中的a"; var name="window"; fu ...