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. Python中Numpy ndarray的使用

    本文主讲Python中Numpy数组的类型.全0全1数组的生成.随机数组.数组操作.矩阵的简单运算.矩阵的数学运算. 尽管可以用python中list嵌套来模拟矩阵,但使用Numpy库更方便. 定义数 ...

  2. java基础—对象转型

    一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...

  3. ARC中__weak;__strong;__unsafe_unretained;修饰词

    测试代码: // Human.h代码 @interface Human : NSObject @property (nonatomic, weak) Cat *pinkCat; @property ( ...

  4. 【原】基于matlab的蓝色车牌定位与识别---绪论

    本着对车牌比较感兴趣,自己在课余时间摸索关于车牌的定位与识别,现将自己所做的一些内容整理下,也方便和大家交流. 考虑到车牌的定位涉及到许多外界的因素,因此有必要对车牌照的获取条件进行一些限定: 一.大 ...

  5. Git基本操作笔记:初始化,用户设置,撤销修改

    1. Git 初始化 git init git  remote add repos_name repos_url git add . git commit -m 'commit message' gi ...

  6. css制作三角形,下拉框三角形

    网站制作中常常需要下拉框,而如果下拉框如果只是单纯的矩形则会显得太过单调,所以这次教大家利用css制作三角形放在矩形上面 首先利用css制作三角形 div { width:0px; height:0p ...

  7. python——全局变量&局部变量

    >>> count = 5 >>> def function(): count = 10 print(10) >>> function() 10 ...

  8. phpMyAdmin关于PHP 5.5+ is required. Currently installed version is: 5.4.16问题

    出现这个提示PHP 5.5+ is required. Currently installed version is: 5.4.16原因可能是: phpmyadmin 版本太新,最小需要php5.5. ...

  9. UVA - 11134 Fabled Rooks问题分解,贪心

    题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...

  10. 带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)

    F. Palindrome Problem Description A string is palindrome if it can be read the same way in either di ...