版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/u013840081/article/details/26180081

题目例如以下:

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
203 Scenario #2
259001
259002
259003
259004
259005
260001

模拟题。这道题题意是对每一个元素。找到它在哪个队,假设已经有队友在排队,他就能够直接排在队友后面,否则排在队伍后面,输出每次出队时的元素。关键是要求入队和出队都是常数时间O(1)(由于command数量非常大)。所以通过遍历找到行号再插入是不行的(复杂度O(N) ),亲測TLE....后来发现用map就能够轻松实现常数时间内找到队伍号(由于能够使队伍号和元素相应起来),而且用二维数组存储结果队伍,第一个维是队伍号,第二个维是元素,这样实现常数时间插入。

肯定对于每支队伍还有队首。队尾指针来实现对 元素的控制。由于出队时是依照顺序一支队伍出队完毕后再出队下一支队伍。所以设置了一个order数组来记录入队的队伍的顺序,用vis来防止一支队伍进入order多次,用变量po来记录当前出队的队伍。easy忽略的细节是假设一支队伍已经出队完了。那它的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. UVA.540 Team Queue (队列)

    UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...

  2. uva 540 - Team Queue(插队队列)

    首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...

  3. UVA 540 Team Queue(模拟+队列)

    题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...

  4. 【UVA - 540】Team Queue (map,队列)

    Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...

  5. Team Queue UVA - 540

      Queues and Priority Queues are data structures which are known to most computer scientists. The Te ...

  6. hdu 1387(Team Queue) STL

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. Team Queue (uva540 队列模拟)

    Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...

  8. ACM题目————Team Queue

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  9. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. c配置库ccl使用小结

    配置文件为key=value键值对形式 下载与安装 库文件下载:ccl-0.1.1.tar.gz 安装:  tar -zxvf ccl-0.1.1.tar.gz  cd ccl-0.1.1 ./con ...

  2. java调用kettle_实现(2)

    (1).参照“java调用kettle_导入jar包(1)”,应用etl工具下lib里的所有jar (2). 最近要对一个系统的数据同步到另一个系统中,要求新系统的数据结果完成之后,实时同步到另一个系 ...

  3. 003Maven_Maven核心概念

    Maven核心概念 Maven插件 Maven的核心仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的每个插件都能实现多个功能,每个功能就是一个插件目标 Maven的生命周期与插件目标相互绑定,以 ...

  4. Cocos2d-x-Lua (2.x)脚本开发之 Lua语言基础

    从今天開始,往后将陆续更新Lua教程,主要是搭载Cocos2dx ,有不论什么疑惑或者不正确的地方.尽情指正.交流.探讨. 那么首先肯定是Lua语言基础的知识点.以下直接附上代码,凝视已经非常清楚.无 ...

  5. js数字格式化(加千分位逗号)

    需求:当金额大于10000时,在作展示的时候,需要加千分位逗号,就是每隔1000要用逗号分隔: 方法一:使用toLocaleString()方法 此方法和toString()方法的区别看这里 < ...

  6. myeclipse10中对象无法点出下面的方法和属性

    刚安装完最新的myeclipse4spring-10.6,发现通过对象无法点出下面的方法和属性,没有自动联想或自动补全,使用“Alt + /”也没得效果,解决方法如下: Window -> Pr ...

  7. hdu 4715(打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4715 思路:先打个素数表,然后判断一下就可以了. #include<iostream> # ...

  8. ThreadLocal并不是一个Thread

    ThreadLocal是什么? 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁 ...

  9. 阿里云CentOS6.8云服务器配置安全组规则

    前提:已经购买阿里云服务器,域名解析也完成了 需要对安全组规则进行配置,才能进行访问 1.进入阿里云首页https://www.aliyun.com/,如下图 2.进入控制台首页,如下图 3.在上图页 ...

  10. shell脚本学习总结02--数组

    bash同时支持普通数组个关联数组,普通数组只能使用整数作为数组的索引,关联数组可以使用字符串作为数组的索引. 数组的定义方法: 在单行中使用一列值定义一个数组 [root@new ~]# array ...