题目如下:

Team Queue 

Queues and Priority Queues are data structures which are known to most computer scientists. TheTeam Queue, however, is not so well known, though it occurs often in everyday life. At lunch timethe 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 thequeue from head to tail to check if some of its teammates (elements of the same team) are alreadyin the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail andbecomes the new last element (bad luck). Dequeuing is done like in normal queues: elements areprocessed 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( ). Then t team descriptions follow, each one consisting of the number of elementsbelonging 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 theimplementation of the team queue should be efficient: both enqueing and dequeuing of an elementshould 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 ablank 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
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
203Scenario #2
259001
259002
259003
259004
259005
260001

模拟题,这道题题意是对每个元素,找到它在哪个队,如果已经有队友在排队,他就可以直接排在队友后面,否则排在队伍后面,输出每次出队时的元素。关键是要求入队和出队都是常数时间O(1)(因为command数量很大),所以通过遍历找到行号再插入是不行的(复杂度O(N) ),亲测TLE....后来发现用map就可以轻松实现常数时间内找到队伍号(因为可以使队伍号和元素对应起来),并且用二维数组存储结果队伍,第一个维是队伍号,第二个维是元素,这样实现常数时间插入。肯定对于每支队伍还有队首,队尾指针来实现对 元素的控制,因为出队时是按照顺序一支队伍出队完成后再出队下一支队伍,所以设置了一个order数组来记录入队的队伍的顺序,用vis来防止一支队伍进入order多次,用变量po来记录当前出队的队伍。容易忽略的细节是如果一支队伍已经出队完了,那它的vis应该变为0,因为接下来可能再入队这个队伍的元素,如果vis不清0的话,这支队伍没法进入order数组,这个细节很隐蔽,因此WA了一次。。最后还是成功AC了,代码并不长。

AC的代码如下:

#include #include
using namespace std;
int ans[1010][1010];
int main()
{
int t,N=0;
while((cin>>t)&&t!=0)
{
N++;
cout<<"Scenario #"<<TEAM,order;
int m,team[1010],row,rear[1010]= {0},fron[1010]= {0},k=0,po=0,vis[1010]= {0};
for(int i=0; i<=t-1; i++)
{
cin>>m;
for(int j=0; j<=m-1; j++)
{
cin>>team[j];
TEAM.insert(make_pair(team[j],i));
}
}
string s;
while(cin>>s)
{
if(s=="ENQUEUE")
{
cin>>m;
row=TEAM[m];
ans[row][rear[row]++]=m;
if(vis[row]==0)
{
order[k++]=row;
vis[row]=1;
}
}
else if(s=="DEQUEUE")
{
cout<<


UVA Team Queue相关推荐

  1. 【队列】Team Queue(luogu-UVA540/poj 2259)

    Team Queue luogu-UVA540 poj 2259 题目大意: 有n个小组的人要排队,每个小组中有若干个人,当一个人入队时,如果队中有自己小组的人就跟在此人后面,否则站在队尾,现在给出一 ...

  2. 540 - Team Queue

    Team Queue PS:因为该题排版较麻烦,这里给出OJ网址:UVa540 - Team Queue 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一 ...

  3. HDU1387 Team Queue

    题目描述: Problem Description Queues and Priority Queues are data structures which are known to most com ...

  4. 解题报告——例题 5-6团体队列(Team Queue UVa 540)——31行代码解决

    题目大意: 有t个团队的人正在排一个长队,每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后.如果没有任何一个队友排队,那么他会排到长队的队尾.输入每个团队中所有队员的编号, ...

  5. UVA - 540:Team Queue

    主要的关键在于:不要试图让所有团队的人在一个队列里面,因为这样如果新入队的是一个前面团队的成员则必须先出队再入队. 应该把每个团队看做一个整体,用一个队列维护团队的顺序,用t个队列维护每个团队内部的顺 ...

  6. 【例题5-6 UVA 540 】Team Queue

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用两个队列模拟就好. 记录某个队在不在队列里面. 模拟 [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #in ...

  7. UVA 540 Team Queue

    题目链接:https://vjudge.net/problem/UVA-540 题目翻译摘自<算法禁赛入门经典> 题目大意 有 t 个团队的人正在排一个长队.每次新来一个人时,如果他有队友 ...

  8. 团体队列(Team Queue,UVA 540)

    题目描述 思路 两类队列,一类是团队的整体队列,此队列存放的是团队的编号,另一类是一个团队一个队列,此队列存放的是队员的编号 DEQUEUE操作:首先根据整体队列,找到第一个团队编号,根据团队编号确定 ...

  9. uva 540 (Team Queue UVA - 540)

    又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...

  10. UVa540 Team Queue

    // 题意:有t个团队的人在排队.每次来了一个新人之后,如果他有队友在排队,那么这个新人会插队到队友的身后. // 要求支持三种指令:ENQUEUE x; DEQUEUE(队首出队); STOP.模拟 ...

最新文章

  1. eclipse问题 - windows版
  2. c 语言程序设计阚道宏,C语言用宏实现静态多态
  3. 'vue' 不是内部或外部命令
  4. 【讨论】拿什么来维护原创作者的权益?
  5. C#8.0的两个有趣的新特性以及gRPC
  6. Springboot Failed to parse configuration class [x]; nested exception
  7. Oracle Grid 下载地址
  8. java excel 表头_Java如何生成excel的表头可变的示例代码
  9. linux中文件权限为d-rwxr-xr,Linux基础知识之文件权限详解
  10. 苹果手机语音备忘录在哪_苹果手机备忘录被家里宝宝误删了如何恢复呢?
  11. 【JavaWeb】火车票管理系统 (三)用户登录-03
  12. 由设计稿一键智能生成代码的快捷软件
  13. MP2459被完美替代内部集成有功率MOSFET管FS2459的60V0.5A降压IC
  14. 开源数库最佳实践-「3306π」社区北京站
  15. StopWatch计时器
  16. Sql Server查询本周及上周(周一-周日)具体某天的数据的sql语句
  17. java学习思维导图(详细)
  18. 执行命令hadoop version出现错误
  19. 解锁编程新姿势——键盘技巧篇
  20. 一加5Android8.0刷机包,一加手机1安卓8.0刷机包放出:刷机小王子神话不灭

热门文章

  1. C#反编译工具:.NET Reflector基础使用
  2. 【BZOJ 3993】【SDOI 2015】星际战争
  3. 亚信安全发现勒索软件新变种 Word文档成为导火索
  4. NPM install报错certificate has expired
  5. Android5.1 快捷开关如何添加和刷新状态
  6. android 修改双卡铃声,Android 修改系统来电铃声
  7. Sql Server数据库中查询操作时“对象名无效”解决方法
  8. 手机web字体css设置,手机web字体css怎么设置微软雅黑
  9. 怎样用VR看分频视频?
  10. 如何实现网页上的气球提示