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. codeforce Gym 100570B ShortestPath Query (最短路SPFA)

    题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...

  2. [学习笔记] Markdown语法备忘

    Markdown语法总结 标题 # 这是一级标题 ## 这是二级标题 ### 这是三级标题 #### 这是四级标题 ##### 这是五级标题 ###### 这是六级标题 注意#后面要加空格 字体 ** ...

  3. Win10开机启动项

    键盘输入:win+r 输入命令:shell:startup

  4. tcp 高性能服务, netty,mqtt

    1. io 线程不要有比较长的服务. 全部异步化. [1] netty 权威指南上只是说业务复杂时派发到业务线程池种. 共用的线程池最好都轻量. 多层线程池后, 下层的可以进行隔离. 这个是 mqtt ...

  5. Bzoj 1131[POI2008]STA-Station (树形DP)

    Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...

  6. [LUOGU] P3952 时间复杂度

    其实,也没那么难写 这种模拟题,仔细分析一下输入格式,分析可能的情况,把思路写在纸上,逐步求精,注意代码实现 主要思路就是算一个时间复杂度,和给出的复杂度比较,这就先设计一个函数把给出的复杂度由字符串 ...

  7. 浅谈 MySQL 中优化 SQL 语句查询常用的 30 种方法

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...

  8. Docker 学习基本操作与守护式容器

    Docker 学习基本操作与守护式容器 容器操作 运行容器 docker run --name指定名字 -istdin -ttty虚拟终端 在终端中用 exit 即可退出容器,并结束运行 查看容器 p ...

  9. strace用法

    strace   --  trace system calls and signals     strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信 ...

  10. vue 顶级组件

    快 有时候懒的把一些通用组件写到template里面去,而业务中又需要用到,比如表示loading状态这样组件. 如果是这样的组件,可以选择把组件手动初始化,让组件在整个app生命周期中始终保持活跃. ...