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. Oracle 11.2.4.0 ACTIVE DATAGUARD 单实例安装(COPY创建备库)

    Oracle 11.2.4.0 ADG 单实例安装(COPY创建备库) 规划: 主: OS: Linux Centos 6.5 X64 hostname:ORA11G-DG1 ipaddress:19 ...

  2. Python-4 变量、字符串

    #1 变量 1)使用前 先赋值 2)命名 字母.数字.下划线 且 不由数字开头 3)大小写不等 4)名字=值 5)尽量选取专业的名字 #2 字符串(文本) 1)字符串两边加引号 2)转义字符 \(反斜 ...

  3. 编译安装带ssl 模块指定版本Python

      出现这个或者fatal error: openssl/名单.h: No such file or directory.都是没有安装libssl-dev- libssl-dev包含libraries ...

  4. eaby技术架构变迁

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 最近在infoq上面看到 ebay介绍其系统架构变迁以及系统设计分享方面的讲座,其中 ...

  5. Spark Streaming架构设计和运行机制总结

    本期内容 : Spark Streaming中的架构设计和运行机制 Spark Streaming深度思考 Spark Streaming的本质就是在RDD基础之上加上Time ,由Time不断的运行 ...

  6. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  7. 用PHP向mysql添加数据

    <?php $name=$_POST['name']; $gender = $_POST['gender']; $age=$_POST['age']; #连接到数据库 $link = mysql ...

  8. Android IOS WebRTC 音视频开发总结(八十)-- NUBOMEDIA: 首个WebRTC PaaS

    本文主要介绍NUBOMEDIA(我们翻译和整理的,译者:jiangpeng,校验:blacker),最早发表在[编风网] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:b ...

  9. PPP(杜撰)

    最开始,电话线入户,用户买来电脑想上网,最简便的方法想办法利用电话线来实现. 最后想了个法子,用电脑的UART的口连到电话线上: 网络提供商一段也用UART为用户提供网络服务: 那么问题来了,UART ...

  10. oracle 客户端单独配置

    本文目的是在CentOS 5.3上安装Oracle 11.2 instant client来访问远端的Oracle 10.2数据库,笔者测试通过,应该也适用于Redhat Linux 5.x     ...