团队队列(列和map结合的经典运用)
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结合的经典运用)的更多相关文章
- 双列集合Map
1.双列集合Map,就是存储key-value的键值对. 2.hashMap中键必须唯一,值可以不唯一. 3.主要方法:put添加数据 getKey---通过key获取数据 keySet- ...
- 双列集合Map的嵌套遍历
双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...
- queue 之团队队列(摘)
有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会排到长队的队尾. 输入每个团队中所有队员的编号,要求支持如下 ...
- python dataframe 针对多列执行map操作
Suppose I have a df which has columns of 'ID', 'col_1', 'col_2'. And I define a function : f = lambd ...
- (10)集合之双列集合Map,HashMap,TreeMap
Map中的元素是两个对象,一个对象作为键,一个对象作为值.键不可以重复,但是值可以重复. 看顶层共性方法找子类特有对象. Map与Collection在集合框架中属并列存在 Map存储的是键值对 Ma ...
- Day 9:双列集合Map及实现该接口的类的常用方法
为什么要学双列集合? 因为单列集合无法处理映射关系,会有成对出现的数据 Map接口 如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复 Map接 ...
- 查询多列得到map与查询得到po对象
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...
- (转)Linux环境进程间通信----系统 V 消息队列列
转:http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.作为早期unix通 ...
- 双列集合Map相关面试题
一.了解Map集合吗?Map集合都有哪些实现 HashMap HashTable LinkedHashMap TreeMap ConcurrentHashMap 二.HashMap和HashTable ...
随机推荐
- Centos小脚本(sftp)
sftp用户创建,改变属组,家目录 #!/bin/python import os,sys class sftp_user(object): def __init__(self,user,passwd ...
- width
position:absolute 其widht:%是想对于最近的已经定位的父元素,如果没有就想对于body widht 是指的内容区的with,设置除了width其他的元素都会使元素变的比width ...
- $《第一行代码:Android》读书笔记——第1章 Android系统
(一)Android系统架构 1.Linux内核层:各种底层驱动,如显示驱动.音频驱动.电源管理等. 2.系统运行库层:各种库支持,如3D绘图.浏览器内核.数据库等. 3.应用框架层:各种API,各种 ...
- 02_虚拟机的安装和SecureCRT、FileZilla、Xmanage、UltraEdit工具的介绍
上述几个工具连接不成功的情况,很多时候是因为ssh服务没有安装,CentOS默认安装,不会出现问题,Ubuntu桌面版默认没有安装,需要手动安装,安装部分参考下文SecureCRT部分 一.安装Cen ...
- 【HackerRank】 The Full Counting Sort
In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...
- Python 循环的综合应用
# 循环综合应用1. # str = "hello,world" 把字符串给反转显示 str = "hello,world" temp = "&quo ...
- ubuntu 忘记root密码
Ubuntu14.04系统中,因为误操作导致管理员密码丢失或无效,并且忘记root密码,此时无法进行任何root/sudo权限操作.可以通过GRUB重新设置root密码,并恢复管理员账户到正常状态. ...
- R语言学习笔记(4)
第四章:基本数据管理 一 贯穿整章的示例 二 变量的创建.重编码和重命名 三 日期值与缺失值 四 数据类型和类型转换 五 数据集的排序.合并与取子集 一 贯穿整章的示例(leadership) ,, ...
- html里id和name的异同
id与name的作用,作为标签的标识符,基本上是一样的. name是老方法,id是在name基础上发明的,比name“现代化”一点,用的范围广一点 <...>中的name原来(刚发明时)就 ...
- HDU4819 Mosaic
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...