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

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. web服务器优化的一些思路

    作为一个新手(并不是菜鸟,而是像我们这样的学生),维护一个网站往往是一个很头疼的问题,尤其是动态网站,更尤其是用java写的网站. 当网站的吞吐量很小的时候你会发现服务器根本不需要维护,因为几乎没有延 ...

  2. git 停止在12% writing objects 怎么回事?

    git 停止在12% writing objects 怎么回事? 输入以下代码试一下: git config --global http.postBuffer 524288000  

  3. MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  4. cf490 C. Hacking Cypher(无语)

    http://codeforces.com/contest/490/problem/C 表示我考场上犯逗.. 这个拆成霍纳边乘边mod即可.. 为毛我考场胡思乱想? #include <cstd ...

  5. 【noip模拟题】挖掘机(模拟题+精度)

    这题直接模拟. 可是我挂在了最后两个点上QAQ.唯一注意的是注意精度啊...用来double后边转成整数就忘记用longlong...sad #include <cstdio> #incl ...

  6. hdu 2686(状压dp)

    题目链接:http://poj.org/problem?id=2686 思路:典型的状压dp题,dp[s][v]表示到达剩下的车票集合为S并且现在在城市v的状态所需要的最小的花费. #include& ...

  7. Boatloader的工作流程

    (1)第一节阶段的功能 1.硬件设备的初始化 2.载入u-boot第二阶段的代码到我们的RAM空间 3.设置好栈 4.跳转到第二阶段的代码入口 (2)第二阶段的功能 1.初始化本阶段所使用的硬件设备 ...

  8. 将list列表中unicode类型的值转换为字符串类型

  9. Linux命令之乐--find

    find是命令行工具箱中最棒的命令之一. 列出当前目录及其子目录中的文件和文件夹. [root@LAMP WebRoot]# find . -print../index.jsp./upload.jsp ...

  10. JSP内置对象——out,get与post

    JSP内置对象是Web容器创建的一组对象,不使用new关键字就可以的内置对象.JSP九大内置对象:out,request,response,session,application,page,pageC ...