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 ...
随机推荐
- Java学习——面向对象【3】
1. 继承 java的类是单继承(一个子类只能继承一个父类),不能多继承(多个父类):A继承自B,A也继承自C,可以多重继承,就是A继承自B,B继承自C(A->B->C), 所有的类都继 ...
- System.TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception. ---> Oracle.DataAccess.Client.OracleException: 提供程序与此版本的 Oracle 客户机不兼容”
.net应用程序通过Oracle.DataAccess.dll访问64位的Oracle服务器,在连接时出现以下异常:“System.TypeInitializationException: The t ...
- Socket网络编程(二)
udp协议发送消息案例 1.创建UdpServer(udp服务器端) package com.cppdy.udp; import java.net.DatagramPacket; import jav ...
- Django注册页面配置设计
一.上次回顾 Django数据的增查改删 models 中有userInfo 三个字段 user password phonenumber,models.userInfo.objects.all(). ...
- uva11916 bsgs算法逆元模板,求逆元,组合计数
其实思维难度不是很大,但是各种处理很麻烦,公式推导到最后就是一个bsgs算法解方程 /* 要给M行N列的网格染色,其中有B个不用染色,其他每个格子涂一种颜色,同一列上下两个格子不能染相同的颜色 涂色方 ...
- hdu1198 普通的并查集
今天开始(第三轮)并查集,,之前学的忘了一些 本题很简单直接上代码 #include<iostream> #include<cstring> #include<cstdi ...
- Spring Boot的Listener机制的用法和实现原理详解
之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...
- 修改ElementUI源码样式
参考:https://segmentfault.com/a/1190000010932321
- 步步为营-104-Lambda语句
1:Lambda的拼接 首先借助一个Lambda的帮助类 using System; using System.Collections.Generic; using System.Linq; usin ...
- 滴水穿石-10GUI
GUI 图形用户界面 1 Frame 窗体 package d10; //第一导入包 import java.awt.Frame; import java.awt.event.WindowAdapte ...