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

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 

  1. 2
  2. 3 101 102 103
  3. 3 201 202 203
  4. ENQUEUE 101
  5. ENQUEUE 201
  6. ENQUEUE 102
  7. ENQUEUE 202
  8. ENQUEUE 103
  9. ENQUEUE 203
  10. DEQUEUE
  11. DEQUEUE
  12. DEQUEUE
  13. DEQUEUE
  14. DEQUEUE
  15. DEQUEUE
  16. STOP
  17. 2
  18. 5 259001 259002 259003 259004 259005
  19. 6 260001 260002 260003 260004 260005 260006
  20. ENQUEUE 259001
  21. ENQUEUE 260001
  22. ENQUEUE 259002
  23. ENQUEUE 259003
  24. ENQUEUE 259004
  25. ENQUEUE 259005
  26. DEQUEUE
  27. DEQUEUE
  28. ENQUEUE 260002
  29. ENQUEUE 260003
  30. DEQUEUE
  31. DEQUEUE
  32. DEQUEUE
  33. DEQUEUE
  34. STOP
  35. 0

Sample Output 

  1. Scenario #1
  2. 101
  3. 102
  4. 103
  5. 201
  6. 202
  7. 203
  8.  
  9. Scenario #2
  10. 259001
  11. 259002
  12. 259003
  13. 259004
  14. 259005
  15. 260001

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

肯定对于每支队伍还有队首。队尾指针来实现对 元素的控制。由于出队时是依照顺序一支队伍出队完毕后再出队下一支队伍。所以设置了一个order数组来记录入队的队伍的顺序,用vis来防止一支队伍进入order多次,用变量po来记录当前出队的队伍。easy忽略的细节是假设一支队伍已经出队完了。那它的vis应该变为0。由于接下来可能再入队这个队伍的元素,假设vis不清0的话,这支队伍没法进入order数组,这个细节非常隐蔽,因此WA了一次。。

最后还是成功AC了,代码并不长。

AC的代码例如以下:

  1. #include
  2. #include
  3. using namespace std;
  4. int ans[1010][1010];
  5. int main()
  6. {
  7. int t,N=0;
  8. while((cin>>t)&&t!=0)
  9. {
  10. N++;
  11. cout<<"Scenario #"< TEAM,order;
  12. int m,team[1010],row,rear[1010]= {0},fron[1010]= {0},k=0,po=0,vis[1010]= {0};
  13. for(int i=0; i<=t-1; i++)
  14. {
  15. cin>>m;
  16. for(int j=0; j<=m-1; j++)
  17. {
  18. cin>>team[j];
  19. TEAM.insert(make_pair(team[j],i));
  20. }
  21. }
  22. string s;
  23. while(cin>>s)
  24. {
  25. if(s=="ENQUEUE")
  26. {
  27. cin>>m;
  28. row=TEAM[m];
  29. ans[row][rear[row]++]=m;
  30. if(vis[row]==0)
  31. {
  32. order[k++]=row;
  33. vis[row]=1;
  34. }
  35. }
  36. else if(s=="DEQUEUE")
  37. {
  38. 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. 第二百五十一节,Bootstrap项目实战--响应式轮播图

    Bootstrap项目实战--响应式轮播图 学习要点: 1.响应式轮播图 本节课我们要在导航条的下方做一张轮播图,自动播放最新的重要动态. 一.响应式轮播图 响应式轮播图 第一步,设置轮播器区域car ...

  2. 以下( )可用于检索session属性userid的值。

    A.session. getAttribute (“userid”); B.session. setAttribute (“userid”); C.request. getParameter (“us ...

  3. (转)FFMPEG-数据结构解释(AVCodecContext,AVStream,AVFormatContext)

    AVCodecContext  这是一个描述编解码器上下文的数据结构,包含了众多编解码器需要的参数信息 如果是单纯使用libavcodec,这部分信息需要调用者进行初始化:如果是使用整个FFMPEG库 ...

  4. 【UVa】Headmaster's Headache(状压dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. angularJs多文件上传

    <input type="file" id="file{{$index}}" class="file{{$index}}" ngf-s ...

  7. POJ2139 Six Degrees of Cowvin Bacon [Floyd]

    水题,随手敲过 一看就是最短路问题,a,b演同一场电影则他们的距离为1 默认全部两两原始距离无穷,到自身为0 输入全部数据处理后floyd 然后照它说的求平均分离度 再找最小的,×100取整输出 #i ...

  8. eq与gt的妙用

    应用到jq中: 一.jquery  :gt选择器: 定义:   :gt 选择器选取 index 值高于指定数的元素. 语法:$(":gt(index)") ex:$("l ...

  9. oracle 存储过程返回结果集

    好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...

  10. POJ3660 传递闭包———floyd算法

    POJ3660 Cow Contest 题目链接:http://poj.org/problem?id=3660 题意:农名约翰有些奶牛,约翰通过让他们决斗来决定他们的排名,约翰让这些奶牛一对一打完一定 ...