团队队列(列和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 ...
随机推荐
- python有哪些好的学习资料或者博客?
推荐Full Stack Python 有各种python资源汇总,从基础入门到各种框架web应用开发和部署,再到高级的ORM.Docker都有.以下是Full Stack Python 上总结的一些 ...
- C# 调用VS自带程序WebDev.WebServer40.EXE 源代码
通过Process.Start启动,VS自带程序WebDev.WebServer40.EXE 在内网架设网站时,为安装IIS条件下用VS自带的小程序来测试效果非常不错! using System; u ...
- 每天一个Linux命令(50)netstat命令
netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. (1)用法: 用法: netstat [选项参数] (2)功能: ...
- ResourceLoader笔记
Ant路径匹配 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配一个字符,如“config?.xml”将匹配“config1.xml”: “*”:匹配 ...
- 5.1深入理解计算机系统——系统级I/O
一.UNIX I/O 在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...
- php执行shell不阻塞方法
大家都知道php执行系统命令的方法有: system() 输出并返回最后一行shell结果. exec() 不输出结果,返回最后一行shell结果,所有结果可以保存到一个返回的数组里面. passth ...
- Elasticsearch6.0简介入门介绍
Elasticsearch简单介绍 Elasticsearch (ES)是一个基于Lucene构建的开源.分布式.RESTful 接口全文搜索引擎.Elasticsearch 还是一个分布式文档数据库 ...
- MapReduce-join连接
join连接 MapReduce能够执行大型数据集间的连接(join)操作.连接操作的具体实现技术取决于数据集的规模及分区方式连接操作如果由mapper执行,则称为“map端连接”:如果由reduce ...
- php 当前时间计算操作
首先要设置时间为中国时区 date_default_timezone_set('PRC'); 对于获取当前时间戳后的各种时间计算 数据库保存最好用时间戳 当前时间time() 上一天 echo dat ...
- linux图形界面基本知识(X、X11、Xfree86、Xorg、GNOME、KDE之间的关系)
linux图形界面基本知识(X.X11.Xfree86.Xorg.GNOME.KDE之间的关系)(转自互联网) LINUX初学者经常分不清楚linux和X之间,X和Xfree86之间,X和KDE,GN ...