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.

InputThe 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.

OutputFor 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
 #include<iostream>
#include<map>
#include<queue>
#include<string>
using namespace std;
int main()
{
int n;
int k = ;
while (cin >> n, n != )
{
k++;
cout << "Scenario #" << k << endl;
int visit[] = { };
map<int, int>team;
queue<int> que;
queue<int> q[];
for (int i = ; i < n; i++)
{
int t,temp;
cin >> t;
while (t--)
{
cin >> temp;
team[temp] = i;
}
}
string sr;
while (cin >> sr, sr != "STOP")
{
if (sr == "ENQUEUE")
{
int tp;
cin >> tp;
q[team[tp]].push(tp);
if (visit[team[tp]] == )
{
visit[team[tp]] = ;
que.push(team[tp]);
}
}
else
{
cout << q[que.front()].front() << endl;
q[que.front()].pop();
if (q[que.front()].empty()) visit[que.front()] = ,que.pop();
}
}
cout << endl;
}
return ;
}

//对于这题,刚开始的思路有问题,总是想着怎么用优先队列解决问题,把思路放在了怎样去定义结构体和怎样定义比较函数,想了半天完全不行,所以在网上看了一下大佬的代码,核心思路就是队列里面放队列,这方法真是简单粗暴,再使用一个数组标记那个队列已放入,厉害,,,,,,

 

Team Queue (HDU:1387)的更多相关文章

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

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

  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. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

  5. C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法

    每次忘记都去查,真难啊 /* C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法 */ /* vector常用用法 */ //头文件 #i ...

  6. Team Queue(多队列技巧处理)

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  8. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  9. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

随机推荐

  1. Python基础知识4--数据结构(树)

    树 树的概念 堂兄弟的双亲不一定是兄弟关系. 二叉树 斜树 满二叉树 完全二叉树 二叉树的性质

  2. 洛谷P4016负载平衡

    题目 负载平衡问题是一个比较经典的网络流问题,但是该问题还有一个数学贪心法. 所以做这个题前,其实可以做一下均分纸牌问题. 均分纸牌问题 均分纸牌问题可以说是作为贪心的入门题. 做法 首先我们应当把原 ...

  3. luoguP4841 城市规划

    题意: 求n个点的无相连通图的个数.有编号 思路一: 黏博客 至于为什么除以k!:(没有博客中说的那么简单) 实际上, 对于一个n的用k个自然数的拆分,每一个拆分的贡献是: $\frac{n!*\Pi ...

  4. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目

    一:创建maven web项目er

  5. 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)

    传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...

  6. SP687 REPEATS - Repeats

    给定字符串,求重复次数最多的连续重复子串. 题目很简单,被细节坑惨了... 前置的一个推论:请看这里. #include <bits/stdc++.h> using namespace s ...

  7. Token防止表单重复提交和CSRF攻击

    Token,可以翻译成标记!最大的特点就是随机性,不可预测,一般黑客或软件无法猜测出来. Token一般用在两个地方: 1: 防止表单重复提交 2: anti csrf攻击(Cross-site re ...

  8. 金融量化分析【day111】:Pandas-时间序列处理

    一.时间对象处理 1.start 开始时间 df["2018-12-01":"2018-12-30"] 2.end 结束时间 df['2018'] ...... ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 A Hard to prepare(递推)

    https://nanti.jisuanke.com/t/31453 题目 有n个格子拉成一个环,给你k,你能使用任意个数的0 ~ 2^k - 1,规定操作 i XNOR j 为~(i  ^  j), ...

  10. 第六节: 六类Calander处理六种不同的时间场景

    背景介绍及其使用 该章节主要补充介绍,在前一章四类触发器的基础上配合六大Canlander来动态删减某些时间,来满足更多的应用场景. 1. DailyCalendar:动态排除某天的某些字段. (需求 ...