题目描述:
Problem Description
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 will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). 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.

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

这一题就是我们昨天比赛的原题 TJU1168
刚开始用链表做的,指来指去都迷了。后来队友说可以用队列实现,学到了队列数组的知识,呵呵,欣慰!
先把每个团体对应一个标号,用该标号对应队列数组。
然后元素入队时先入队列数组,如果该队列没在总的队列中,就把该队列的标号压入总队列,对队列的标号进行标记。
出队是出的是总队列队首标号指向的队列数组中的队列的队首元素,如果出队后该队列为空,总队列队首出队,该队列的标号恢复为未标记。

代码

#include < iostream >
#include < queue >
#include < string >
#include < map >
#pragma warning (disable:4786)
using namespace std;
int main()
{
int i,n,m,t,ca = 1 ;
bool visit[ 1002 ];
char str[ 20 ];
while (scanf( " %d " , & n),n)
{
queue < int > q[ 1002 ],que;
map < int , int > team;
for (i = 0 ;i < n;i ++ )
{
scanf( " %d " , & m);
while (m -- )
{
scanf( " %d " , & t);
team[t] = i;
}
}
memset(visit, 0 , sizeof (visit));
printf( " Scenario #%d\n " ,ca ++ );
while (scanf( " %s " ,str))
{
if (strcmp(str, " STOP " ) == 0 )
{
puts( "" );
break ;
}
else if (strcmp(str, " ENQUEUE " ) == 0 )
{
scanf( " %d " , & t);
q[team[t]].push(t);
if (visit[team[t]] == false )
{
que.push(team[t]);
visit[team[t]] = true ;
}
}
else
{
printf( " %d\n " ,q[que.front()].front());
q[que.front()].pop();
if (q[que.front()].empty())
{
visit[que.front()] = false ;
que.pop();
}
}
}
}
return 0 ;
}

然后下一步该主攻《数论》了,要加油打个漂亮仗啊!!

转载于:https://www.cnblogs.com/DreamUp/archive/2010/07/22/1783004.html

HDU1387 Team Queue相关推荐

  1. 【队列】Team Queue(luogu-UVA540/poj 2259)

    Team Queue luogu-UVA540 poj 2259 题目大意: 有n个小组的人要排队,每个小组中有若干个人,当一个人入队时,如果队中有自己小组的人就跟在此人后面,否则站在队尾,现在给出一 ...

  2. 540 - Team Queue

    Team Queue PS:因为该题排版较麻烦,这里给出OJ网址:UVa540 - Team Queue 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一 ...

  3. UVa540 Team Queue

    // 题意:有t个团队的人在排队.每次来了一个新人之后,如果他有队友在排队,那么这个新人会插队到队友的身后. // 要求支持三种指令:ENQUEUE x; DEQUEUE(队首出队); STOP.模拟 ...

  4. 解题报告——例题 5-6团体队列(Team Queue UVa 540)——31行代码解决

    题目大意: 有t个团队的人正在排一个长队,每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后.如果没有任何一个队友排队,那么他会排到长队的队尾.输入每个团队中所有队员的编号, ...

  5. UVA - 540:Team Queue

    主要的关键在于:不要试图让所有团队的人在一个队列里面,因为这样如果新入队的是一个前面团队的成员则必须先出队再入队. 应该把每个团队看做一个整体,用一个队列维护团队的顺序,用t个队列维护每个团队内部的顺 ...

  6. POJ 2259 team queue团队排队问题

    题目链接: http://poj.org/problem?id=2259 题目大意: 告诉你一堆人(m个人是一组的,n个人是一组的....):然后一个人来排队了,先看下有自己组的熟人吗?有的话直接排在 ...

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

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

  8. Poj2259 Team Queue 【队列】

    http://poj.org/problem?id=2259 队列是一种先进先出的数据结构.它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称作队尾 ...

  9. UVA 540 Team Queue

    题目链接:https://vjudge.net/problem/UVA-540 题目翻译摘自<算法禁赛入门经典> 题目大意 有 t 个团队的人正在排一个长队.每次新来一个人时,如果他有队友 ...

最新文章

  1. ap忘记管理ip地址怎么办_什么是无线AP?胖瘦AP如何区分?
  2. java登录界面命令_Java命令行界面(第18部分):JCLAP
  3. 中科慧眼双目安装pcl
  4. Python的内置函数的学习笔记
  5. 火狐浏览器插件大全,火狐插件大全,firefox插件使用方法
  6. 【HDOJ6957】Maximal submatrix(单调栈,最大子矩阵面积)
  7. 從零開始學 ReactJS:最完整的 React 生態系入門教程
  8. 02-微信小程序商城 顶部广告图片(微信小程序商城开发、小程序毕业设计、小程序源代码)(黄菊华-微信小程序开发教程)
  9. Python 实现Tracert追踪TTL值
  10. CMT2300A 433MHz SUB-1G无线收发芯片
  11. 软考新思维--2017年上半年信息系统项目管理师上午试题分析与答案(试题1-5题)
  12. 合肥耀之阳电商:店铺商品属性怎么样修改
  13. 获取图片上任意一点的颜色值
  14. 安装redis时被意外攻击 newinit
  15. Android自定义折线图,可设置基准线,不同点颜色
  16. PostgreSQL 14和SCRAM认证的改变--应该迁移到SCRAM?
  17. 响应式网站建设有什么优势?
  18. 洛谷 P3978 [TJOI2015]概率论
  19. 树莓派ADC(ADS1115)读取Voltage Sensor(电压检测模块)
  20. 知识见解关于高斯定理

热门文章

  1. navicat怎么查看数据库的url_Navicat使用HTTP通道连接MySQL
  2. 卷王:大厂周报已经长达30页
  3. Pyramid Real Image Denoising Network-金字塔实像去噪网络
  4. 人际关系以及处理矛盾
  5. android——高效调用系统图库
  6. 读书笔记-沟通的方法
  7. 斗牛太危险,来斗HEXA吧
  8. LFSR python实现
  9. 技术分享 | mysqlbinlog 技巧
  10. 青少年软件编程C++二级题库(51-60)