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 file 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. 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’, 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

Sample Output

Scenario #1

101 102 103 201 202 203

意思就是很多团队相互排队,若你的团队中有人在队伍中,就可以插到你团队的最后面否则就只能插到队伍的最后面!!

这题map的使用和俩个queue队列使用时关键,map<int ,int >team有效的将成员和团队编号链接了起来,就可以进行t=team[x]的对应了。而queue <int >q,q2[maxn]代表俩个队伍,一个为总队伍中的团队编号队伍,二是队伍中的团队小队伍,

接下来照着题目操作即可

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int main()
{ int n;
int fl=;
while(cin>>n)
{ int flag=;
map <int,int> team;
if(n==) break;
cout<<"Scenario #"<<fl++<<endl;
while(n--)
{
int t,x;
cin>>t;
for(int i=;i<t;i++)
{
cin>>x;
team[x]=flag;
}
flag++;
}
queue<int>q,q2[maxn];
for(;;)
{
int x;
char cmd[];
cin>>cmd;
if(cmd[]=='S') break;
else if(cmd[]=='D')
{
int t=q.front();
cout<<q2[t].front()<<endl;
q2[t].pop();
if(q2[t].empty()) q.pop();
}
else if(cmd[]=='E')
{ cin>>x;
int t=team[x];
if(q2[t].empty()) q.push(t);
q2[t].push(x);
} }
cout<<endl;
}
return ;
}

团队队列(列和map结合的经典运用)的更多相关文章

  1. 双列集合Map

    1.双列集合Map,就是存储key-value的键值对. 2.hashMap中键必须唯一,值可以不唯一. 3.主要方法:put添加数据    getKey---通过key获取数据    keySet- ...

  2. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  3. queue 之团队队列(摘)

    有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会排到长队的队尾. 输入每个团队中所有队员的编号,要求支持如下 ...

  4. python dataframe 针对多列执行map操作

    Suppose I have a df which has columns of 'ID', 'col_1', 'col_2'. And I define a function : f = lambd ...

  5. (10)集合之双列集合Map,HashMap,TreeMap

    Map中的元素是两个对象,一个对象作为键,一个对象作为值.键不可以重复,但是值可以重复. 看顶层共性方法找子类特有对象. Map与Collection在集合框架中属并列存在 Map存储的是键值对 Ma ...

  6. Day 9:双列集合Map及实现该接口的类的常用方法

    为什么要学双列集合? 因为单列集合无法处理映射关系,会有成对出现的数据 Map接口  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复 Map接 ...

  7. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  8. (转)Linux环境进程间通信----系统 V 消息队列列

    转:http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.作为早期unix通 ...

  9. 双列集合Map相关面试题

    一.了解Map集合吗?Map集合都有哪些实现 HashMap HashTable LinkedHashMap TreeMap ConcurrentHashMap 二.HashMap和HashTable ...

随机推荐

  1. 谷歌机器学习速成课程---3降低损失 (Reducing Loss):梯度下降法

    迭代方法图(图 1)包含一个标题为“计算参数更新”的华而不实的绿框.现在,我们将用更实质的方法代替这种华而不实的算法. 假设我们有时间和计算资源来计算 w1 的所有可能值的损失.对于我们一直在研究的回 ...

  2. pyhton3 configparser模块

    1 #!/usr/bin/env python 2 # coding=utf-8 3 __author__ = 'Luzhuo' 4 __date__ = '2017/5/26' 5 # config ...

  3. 012_Eclipse中使用 HDFS URL API 事例介绍

    本事例其实和使用hdfs FileSystem API差不多,FileSystem API也是通过解释成URL在hdfs上面执行的,性质相同,但是实际中用 的fFileSystem会多一点,源码如下: ...

  4. springboot获取URL请求参数的几种方法

    原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...

  5. 容器排序之sort,stable_sort

    bool isShorter(const string &s1, const string &sz){ return s1.size() < sz.size(); } int m ...

  6. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...

  7. CSS实现三角形图标的原理!!!!今天总算弄懂了。

    网页中经常有一种三角形的图标,鼠标点一下会弹出一个下拉菜单之类的(之前淘宝也有,不过现在改版好像没有了) 之前以为是个png图标背景,后来在bootstrap中看到有一个图标样式叫做caret的用来实 ...

  8. Nagios 服务安装

    Nagios 环境部署 安装服务包 操作系统:Linux Centos 6.4 32位 安装包:nagios-3.4.3.tar.gz 安装包:nagios-plugins-1.4.13.tar.gz ...

  9. 跨平台移动开发_PhoneGap API Camera 使用摄像头采集照片.

    camera对象提供对设备默认摄像头应用程序的访问. 程序运行效果 相关代码 <!DOCTYPE html> <html> <head> <title> ...

  10. https网站无法加载http路径的js和css

    在https的网站中引用http路径的js或css会导致不起作用,其形如: <script src="http://code.jquery.com/jquery-1.11.0.min. ...