Team Queue

Time Limit:                                                        3000MS                           Memory Limit: Unknown   64bit IO Format:                            %lld & %llu                        

Submit                                        Status

Description

 

Queues and Priority Queues are data structures which are known to most computer scientists. TheTeam 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 itsteammates (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 file will contain one or more test cases. Each test case begins with the number of teamst (   
). 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.

Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

Output

For each test case, first print a line saying ``   Scenario #k", wherek 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

题意:先给出队伍个数以及每队成员的id,然后进行入队、出队操作,出队时输出出队的人,但是入队规则不同,若已有同队的排在前面,则直接排到最后一个同队的人的后面,即有认识的人就可以插队。因此用queue q记录团队的顺序,用queue que数组记录一支队伍的对内顺序,队伍号则用map记录。另外之前纠结修改半天纠结先判断队伍内人数是否为空还是先pop,后来发现不需要,因为你一支队伍的顺序由队员决定,只有存在队员才会有这支队伍,因此队员数>=1,因此先pop,再判断。此题还拓展了queue的数组,队列居然还有数组......醉了

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
using namespace std;
int main(void)
{
int t,n,i,j,cnt,T=0,idd,num;
string ops;
while (cin>>t&&t)
{
map<int,int> tlist;//队伍名记录
queue<int> q,que[1010];//队伍之间的顺序和队内人数顺序记录
T++;//计数器
idd=0;
while (t--)//记录队伍以及队员所属
{
cin>>n;
while (n--)
{
cin>>num;
tlist[num]=idd;
}
idd++;
}
cout<<"Scenario #"<<T<<endl;//输出当前case数
cnt=0;
while (cin>>ops&&ops!="STOP")
{
if(ops[0]=='E')//入队
{
cin>>num;
i=tlist[num];//这个人属于哪支队伍?
if(que[i].empty())//你前面没人即第一个到,那你来卡位
{
q.push(i);//签到
}
que[i].push(num);//进入对应的队伍
}
else if(ops[0]=='D')//出队
{
int f=q.front();队伍内第一个人
cout<<que[f].front()<<endl;
que[f].pop();//出队
if(que[f].empty())//出完没人了
q.pop();//换下一队
}
}
cout<<endl;
}
return 0;
}

UVa——540Team Queue(STL练习map、queue数组的综合使用)的更多相关文章

  1. Java中的容器类(List,Set,Map,Queue)

    Java中的容器类(List,Set,Map,Queue) 一.基本概念 Java容器类类库的用途是“保存对象”,并将其划分为两个不同的概念: 1)Collection.一个独立元素的序列,这些元素都 ...

  2. stl容器学习——queue,stack,list与string

    目录 头文件 string 目录部分 1.string的定义及初始化 ① 用一个字符串给另一个字符串赋值 ②用字符串常量对字符串进行赋值 ③ 用n个相同的字符对字符串赋值 2.string的运算符及比 ...

  3. queue STL

    //queue STL //queue is just a container adaptor, which is a class that use other container. //just l ...

  4. hdoj--5233--Gunner II(map+queue&&二分)

     Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  5. c++ STL:队列queue、优先队列priority queue 的使用

    说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...

  6. C++标准模板库(STL)之Queue

    1.Queue的常用用法 queue:队列,实现的一个先进先出的容器. 1.1.queue的定义 使用queue,首先要加头文件#include<queue>和using namespac ...

  7. STL中队列queue的用法

    头文件:#include <queue> 建立一个队列queue < 类型 > q 加入一个新的元素q.push(a) 询问队首元素q.front() 弹出队首元素q.pop( ...

  8. [STL] Implement "map", "set"

    练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...

  9. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  10. C++ STL 中 map 容器

    C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...

随机推荐

  1. Android(java)学习笔记136:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1. 首先项目图: 2. 这里的布局文件activity_main.xml: <LinearLayout xmlns:andro ...

  2. DROP OPERATOR - 删除一个操作符

    SYNOPSIS DROP OPERATOR name ( lefttype | NONE , righttype | NONE ) [ CASCADE | RESTRICT ] DESCRIPTIO ...

  3. python之道13

    看代码分析结果 func_list = [] for i in range(10): func_list.append(lambda :i) v1 = func_list[0]() v2 = func ...

  4. java基础——冒泡排序

    最近开始准备面试,所以将Java基础复习一遍,又学习了冒泡排序 冒泡排序的基本思想是,对相邻的元素进行两两比较,顺序相反则进行交换,这样,每一趟会将最小或最大的元素“浮”到顶端,最终达到完全有序 ja ...

  5. 01_13_Struts_默认Action

    01_13_Struts_默认Action 1. 配置struts默认Action <package name="default" namespace="/&quo ...

  6. XML 转 fastJSON

      import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Doc ...

  7. linux文件属性之linux文件删除原理

    Linux是通过link的数量来控制文件删除的,只有当一个文件不存在任何link的时候,这个文件才会被删除.一般来说,每个文件都有2个link计数器:i_count和i_nlink. i_count的 ...

  8. 【mysql】mysql has gone away

    原文 http://www.jb51.net/article/23781.htm MySQL server has gone away 问题的解决方法 投稿:mdxy-dxy 字体:[增加 减小] 类 ...

  9. python3 兔子繁殖问题

    题目 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 代码: month = int(input("繁殖 ...

  10. python解析库之 XPath

    1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename    选取此节点的所有子节点 /                    从当前 ...