UVA 540(队列)

时间:2022-09-06 08:20:13

Description

UVA 540(队列)

 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 ( UVA 540(队列)). 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&period;540 Team Queue &lpar;队列&rpar;

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

  2. UVA 540 Team Queue(模拟&plus;队列)

    题目代号: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 &lpar;团体队列&rpar; Team Queue

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

  5. 【UVA - 540】Team Queue (map&comma;队列)

    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 &lpar;Team Queue UVA - 540&rpar;

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

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

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

随机推荐

  1. c&plus;&plus;中resize函数怎么用

    resize(),设置大小(size);reserve(),设置容量(capacity);size()是分配容器的内存大小,而capacity()只是设置容器容量大小,但并没有真正分配内存.打个比方: ...

  2. Spring第十一篇——–Spring整合Hibernate之配置数据源

    DataSource(数据源)提供了一个标准化的取得数据库连接的方式,通过getConnection()方法即可取得数据库的连接,Spring也提供了数据库连接池(DataBase connectio ...

  3. 64位Ubuntu 13&period;04 安装Bochs 2&period;3&period;5

    bochs 2.3.5源码编译 网上编译bochs的资料非常多,基本的问题都有解决方案,我重点讲不常见的问题. 基本安装步骤 tar vxzf bochs-2.3.5.tar.gz cd bochs- ...

  4. untiy 插件工具: 游戏中 策划数据Excel 导出到项目中

    https://github.com/zhutaorun/Excel2Unity,这个项目是直接下载就可以用的, 其中原理和相关的解释 http://blog.csdn.net/neil3d/arti ...

  5. POJ 1741&sol;1987 树的点分治

    树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况. #include <iostream> #include <vector> #i ...

  6. Java对字符串进行的操作

    本篇总结归纳对字符串或数组进行相关操作问题 数组倒序输出 查找字符串中第一次重复的字符 查找字符串中第一次没有重复的字符 删除字符串中重复的元素 倒序输出问题 第一种:对于数组 public int[ ...

  7. 【原】Java学习笔记010 - 数组

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:一堆分数,要 ...

  8. &lbrack;转&rsqb;NSIS 制作安装包无法创建桌面快捷方式或无法删除开始菜单项

    用户将桌面文件转移了,如: D:\Doc\Desktop  ,安装程序后,桌面不会生成快捷方式, 或者卸载后,开始菜单中的文件也不会被删除 NSIS卸载后无法删除开始菜单中的内容原因:因为NSIS中使 ...

  9. HashTable代码解析

    HashTable继承关系如下: HashTable是一个线程安全的[键-值对]存储结构.其存储结构和HashMap相同,参考这里. 1. HashTable定义了一个类型为Entry<K,V& ...

  10. PHP分多步骤填写发布信息的简单方法实例代码

    1.php 复制代码 代码如下: <form name=form1 id=form1 method=post action=2.php> 基本信息1:<input type=text ...