Description

 Team Queue 

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life.
At lunch time the 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 the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right
behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed 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 elements belonging 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 the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should 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 a blank 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

Miguel A. Revilla

1999-01-11


队列应用

题意:让你排一个长队。ENQUEUE -X 就是往队列里加一个元素,假设这个元素跟队列里的某个元素在上边给出的team里是在同样的team,则将这个元素插在这个元素的右边,否则放在总队列的最后 。

用一个队列保存队伍编号,还有一些队列保存编号每一个队的队员。

#include<stdio.h>
#include<algorithm>
#include<cctype>
#include<queue>
#include<string.h>
using namespace std;
int order[1000001],exist[1000001];
queue<int>team[1001];
queue<int>cnt; int main()
{
int t,cas=1;
//freopen("in.txt","r",stdin);
char s[15];
while(~scanf("%d",&t)&&t)
{
int n=0,num;
for(int i=0;i<t;i++)
{
scanf("%d",&n);
for(int j=1;j<=n;j++)
{
scanf("%d",&num);
order[num]=i;
}
}
for(int i=0;i<=t;i++)
{
while(!team[i].empty())team[i].pop();
}
while(!cnt.empty())cnt.pop();
memset(exist,0,sizeof(exist));
printf("Scenario #%d\n",cas++);
while(~scanf("%s",s))
{
if(strcmp(s,"STOP")==0)break;
else if(strcmp("DEQUEUE",s)==0)
{
int p=cnt.front();
int ans=team[p].front();team[p].pop();
printf("%d\n",ans);
if(team[p].empty())
{
cnt.pop();
exist[p]=0;
}
}
else
{
scanf("%d",&num);
int &temp=order[num];
team[temp].push(num);
if(!exist[temp])
{
cnt.push(temp);
exist[temp]=1;
}
}
}
printf("\n");
}
return 0;
}

UVA 540(队列)的更多相关文章

  1. UVA.540 Team Queue (队列)

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

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

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

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

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

  4. UVa 540 (团体队列) Team Queue

    题意: 每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后.否则站到整个队伍的队尾. 输出每次出队的人的编号. 分析: 容易看出,长队中,在同一个团体的人是排在 ...

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

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

  6. UVa 540 Team Queue 【STL】

    题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...

  7. UVA 540 Team Queue

    思路:使用优先队列,按队伍出现的时刻和自身出现的时刻定义优先级,同时记录此时刻队列里是否有自己队伍的人,一开始没注意,wa了两发. #include<map> #include<qu ...

  8. uva 540 (Team Queue UVA - 540)

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

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

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

随机推荐

  1. Struts2知识整理

    准备找工作了.好忐忑!!! 整理整理知识,好好准备. 其实现在Struts2好像不是特别流行,不过还是有用武之地的. struts2简介 struts2是基于mvc开发模型的框架,属于表现层框架 核心 ...

  2. no suitable driver found for jdbc:mysql//localhost:3306/..

      出现这样的情况,一般有四种原因(网上查的): 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql ...

  3. HTML知识速递

    1.html的定义 超文本标记语言(Hypertext Markup Language,HTML)通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则 浏览器按顺序渲染网页文件,然后 ...

  4. selenium-iframe操作

    iframe,又叫浮动帧标记,是内嵌的网页元素,可以将一个html文件嵌入到另一个html文件中显示. 最典型的案例就是163邮箱登录:http://mail.163.com/,被绿色覆盖的部分即为i ...

  5. ab使用命令

    ab使用-A auth-username:password    向服务器提供基本认证信息.用户名和密码之间":"分割,以base64编码形式发送.无论服务器是否需要(即是否发送了 ...

  6. Get started with Google Analytics

    What is Google Analytics Google Analytics is a Google official analytics tool that is primarily used ...

  7. JDBC数据库编程

    常识名词:ODBC ,JDBC,JDBC API ,JDBC Driver API  数据准备,续上节:   JDBC编程流程 最基本的JDBC操作 本段内容主要完成JDBC的增删查改操作 packa ...

  8. SparkStreaming动态读取配置文件

    SparkStreaming动态读取配置文件 标签: SparkStreaming HDFS 配置文件 MySql 需求 要实现SparkStreaming在流处理过程中能动态的获取到配置文件的改变 ...

  9. MySQL执行一个查询的过程

    总体流程 客户端发送一条查询给服务器: 服务器先会检查查询缓存,如果命中了缓存,则立即返回存储在缓存中的结果.否则进入下一阶段: 服务器端进行SQL解析.预处理,再由优化器生成对应的执行计划: MyS ...

  10. python中print()函数的“,”与java中System.out.print()函数中的“+”

    python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能. python中: print("hello,world!") 输出 ...