团队队列(列和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 ...
随机推荐
- java基础—— Collections.sort的两种用法
package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...
- Cgroups控制cpu,内存,io示例【转】
本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...
- How to Google
程序员的基础生存技能 -- 关于搜索引擎的小贴士 如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接 ...
- Android电容屏(一)【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7820492 关键词:Android 电容屏 tp ITO 平台信息:内核:linu ...
- RHCE学习笔记 管理1 (第一、二章)
第一章 命令行访问 1.Ctrl+alt+F2~F6 切到虚拟控制台,ctrl+alt+F1 回到图形界面 2.格式 : 命令 选项 参数 [] 为可选项目 ...表示该项目任意 ...
- 普通神经网络和RNN简单demo (一)
2017-08-04 花了两天时间看了下神经网络的一点基础知识,包括单层的感知机模型,普通的没有记忆功能的多层神经网咯,还有递归神经网络RNN.这里主要是参考了一个博客,实现了几个简单的代码,这里把源 ...
- Android深度探索(卷1)HAL与驱动开发 虚拟环境的安装
前言: 最近在看<Android深度探索(卷1)HAL与驱动开发>安装随书带的虚拟环境浪费了很多时间,说是虚拟环境的安装倒不如说是虚拟环境的导入,其实没什么技术含量,也没有什么复杂的,只是 ...
- lua 写逻辑打印log时,打印到一半后停止不再打印,程序停止
问题描述:ubuntu下用lua开发游戏电子邮件模块,自己测试时向用户推送100封,而用户最多只能有50封.这是调用sysdelete删除一些邮件.当打印log时,打印到一半后程序中途停止.将打印lo ...
- VS10_慢_优化
参考网址: http://blog.csdn.net/cll131421/article/details/15341367 1. 我暂时只做了这个: “ 一.VS2010选项视觉体验设置 工具-> ...
- HTML5 拖放---drag和drop
拖放四步走:第一步:设置元素可拖放,即把 draggable属性设置为 true: 例:<div id="div" draggable="true"&g ...