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

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. USB转TTL 下载线 线序定义

    产品名称:USB转TTL 下载线 长度       :1米 芯片       :PL2303HX.PL2303TA. 线序定义红+5V, 黑GND, 绿TXD,白RXD

  2. hive表支持中文设置

    默认创建表时说明中带有中文字段时会显示如下乱码信息: 解决方案: 在hive的元数据库中执行以下SQL语句,然后重新创建刚才的表即可 . ) character set utf8; ) charact ...

  3. Trie树 + DFS - CSU 1457 Boggle

    Boggle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有m个询问. 每个询问 ...

  4. ComBoFuzzySearch.js

    /** * combobox和combotree模糊查询 */(function () { //combobox可编辑,自定义模糊查询 $.fn.combobox.defaults.editable ...

  5. Java Print 打印

    Java 原生的API中有Print,使用Print可以操作打印机进行打印操作,获取打印机属性,下面是代码 打印程序(静默打印) package com.boci.PrintPDF; import j ...

  6. ETL概念,ETL流程

    ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...

  7. centos7 docker install

    env: os :centos 7 vmware steps: 1.yum  -y install docker after installed ,using   docker version cmd ...

  8. NIO概览

    NIO专题:http://developer.51cto.com/art/201112/307172.htm 一.新IO概述: 新IO和传统IO都是用于进行输入/输出,相比于传统IO面向流的处理方式, ...

  9. PHP 开发环境的搭建和使用03-- 安装mySql

    1/  安装的MySQL版本是5.6.10版本的,直接点击Install 2/ 选择 Execute 3/  更新最新版本成功后,选择 "next" 4/  自定义安装方式,选择C ...

  10. 在nginx启动后,如果我们要操作nginx,要怎么做呢 别增加无谓的上下文切换 异步非阻塞的方式来处理请求 worker的个数为cpu的核数 红黑树

    nginx平台初探(100%) — Nginx开发从入门到精通 http://ten 众所周知,nginx性能高,而nginx的高性能与其架构是分不开的.那么nginx究竟是怎么样的呢?这一节我们先来 ...