Team Queue POJ - 2259 (队列)
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
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
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 题意:n个小队,每个小队若干人。入队时,若队伍中已经有了该小队的人就直接排在其后面,没有的话就排在队伍后面。有入队和出队两种操作。
思路:用队列进行模拟,map记录每个人属于哪支队伍,然后que【0】,记录队伍的顺序,que【i】记录该队伍的人。 1 <= i <= 1000
#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
using namespace std; int t;
queue<int>que[];
map<int,int>mp;
int main()
{
int cas = ;
while(~scanf("%d",&t) && t)
{
mp.clear();
for(int i=;i<=;i++)
{
while(!que[i].empty())que[i].pop();
}
int cnt = ;
while(t--)
{
int k,tmp;
scanf("%d",&k);
++cnt;
for(int i=;i<=k;i++)
{
scanf("%d",&tmp);
mp[tmp] = cnt;
}
}
char s[];
printf("Scenario #%d\n",++cas);
while(~scanf("%s",s) && s[] != 'S')
{
if(s[] == 'E')
{
int tmp;
scanf("%d",&tmp);
if(que[mp[tmp]].empty())que[].push(mp[tmp]);
que[mp[tmp]].push(tmp);
}
else
{
if(!que[].empty())
{
int tmp = que[].front();
printf("%d\n",que[tmp].front());
que[tmp].pop();
if(que[tmp].empty())que[].pop();
}
}
}
puts("");
}
}
Team Queue POJ - 2259 (队列)的更多相关文章
- Team Queue(POJ 2259)
题意:有若干个团体,每个团体有若干个元素,他们按次序来排队,如果队列中已经有同一团体的元素在,则可以插队到它后面,模拟这个过程 思路:用map存下元素与团体的关系,并开2个队列,一个存整体队伍的排列( ...
- Team Queue(多队列技巧处理)
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...
- 【UVA - 540】Team Queue (map,队列)
Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...
- UVA 540 Team Queue(模拟+队列)
题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...
- queue POJ 2259 Team Queue
题目传送门 题意:先给出一些小组成员,然后开始排队.若前面的人中有相同小组的人的话,直接插队排在同小组的最后一个,否则只能排在最后面.现在有排队和出队的操作. 分析:这题关键是将队列按照组数分组,用另 ...
- POJ 2259 - Team Queue - [队列的邻接表]
题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...
- POJ 2259 Team Queue(队列)
题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...
- poj 2259 Team Queue
Team Queue Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2977 Accepted: 1092 Descri ...
随机推荐
- easyui生成合并行,合计计算价格
easyui生成合并行,合计计算价格 注:本文来源: 原创 一:图样你效果图 二:代码实现 1:datagrid 列展示: window.dataGrid = $("#dataGrid&qu ...
- Confluence 6 workbox 配置查询间隔
查询间隔在Confluence 服务器中的 workbox 被用来显示应用内通知和任务. 激活的查询间隔(Active polling interval) Confluence 将会等待多少时间(秒) ...
- 基于 Confluence 6 数据中心在你的 Atlassian 应用中配置 SAML 授权
希望在 Confluence 中配置SAML: Go to > 基本配置(General Configuration) > SAMl 授权(SAML Authentication). 选 ...
- Intenet 地址
java.net.InetAddress类是java对Ip地址(包括ipv4和ipv6)的高层表示,大多数其他网络类都要用到这个类,包括Socket, ServerSocket, URL, Datag ...
- 【Vue】组件watch props属性值
转载: https://www.cnblogs.com/mqxs/p/8972368.html #HTML <div id="example"> <p> & ...
- js call() 笔记
var ctrl = function() {}; ctrl.view = function() { return { show: function() { console.log("vie ...
- 论文阅读笔记三十:One pixel attack for fooling deep neural networks(CVPR2017)
论文源址:https://arxiv.org/abs/1710.08864 tensorflow代码: https://github.com/Hyperparticle/one-pixel-attac ...
- 关于HTML或JS加密解密的七种方式
本文一共介绍了七种方法: 一:最简单的加密解密 二:转义字符""的妙用 三:使用Microsoft出品的脚本编码器Script Encoder来进行编码 (自创简 ...
- Python零基础入门之Tkinter的对话框
这篇博客主要是总结一下Tkinter中的对话框的使用,值得一提的是自从python3.0之后关于关于对话框的模块(messagebox.filedialog.colorchooser)都被收归到了tk ...
- python 给对象绑定属性和方法和__slots__的使用
# 以c语言为主是静态语言,运行之前先编译,在运行的过程中不允许编辑代码# 在运行的过程中,可以改变,可以添加属性,就是属于动态语言(python) # python动态的添加属性以及方法class ...