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 ...
随机推荐
- js——正则表达式
1. 创建一个正则表达式 var patt=new RegExp(pattern,modifiers);var patt=/pattern/modifiers; var index = str.sea ...
- 安卓易学,爬坑不易—腾讯老司机的RecyclerView局部刷新爬坑之路
前言 安卓开发者都知道,RecyclerView比ListView要灵活的多,但不可否认的里面的坑也同样埋了不少人.下面让我们看看腾讯开发工程师用实例讲解自己踩坑时的解决方案和心路历程. 话说有图有真 ...
- 2。创建第一个angular应用,已经开发前的一些配置
现在我们开始新建一个angular5的脚手架 . 到想要建项目的目录下.比如我的 在 D:\JsProjects 进入cmd或者powershell cd 进入该文件夹下 然后开始新建,ng ne ...
- hdu4990 转移矩阵
找了半天错发现m有可能是1.. /* 如果n是奇数,就进行(n/2)次转移,然后取F[2],反之取F[1] */ #include<bits/stdc++.h> using namespa ...
- bzoj 4007
非常好的树形dp 首先,有个很显然的状态:记状态f[i][j]表示以i为根节点的子树中选了j个叶节点作战,那么很显然有转移:f[i][j1+j2]=f[i<<1][j1]+f[i<& ...
- 20165314 2016-2017-2 《Java程序设计》第3周学习总结
20165314 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 类体包含成员变量和域变量 局部变量只在方法内有效 对象的创建以及对象对自己变量和方法通过用. ...
- SQL语法汇总
以下默认为mySQL与SQLsever都能使用SQLsever不能使用的另外标出来了 显示数据库SHOW DATABASES;进入其中一个数据库USE students;显示进入数据库中的所有表SHO ...
- Python面向对象 三大特性 综合案例+1(视频里的作业)
class Dog: # 在创建一个小狗实例的时候,给它设置几个属性 def __init__(self, name, age = 1): self.name = name self.age = ag ...
- OpenCV-Python入门教程5-阈值分割
一.固定阈值分割 import cv2 import matplotlib.pyplot as plt # 灰度图读入 img = cv2.imread('gradient.jpg', 0) # 阈值 ...
- react-native 之gradle-2.x-all.zip 下载缓慢或失败
去官网http://www.gradle.org/downloadshttp://services.gradle.org/distributions下载匹配的 Gradle 版本把zip直接放到C:\ ...