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. YCD 软件更新方法

    备份Messenger的数据库和Player的Chainmail数据 Upgrade process in Cnario is quite simple, uninstall old version ...

  2. git branch --set-upstream 本地关联远程分支

    最近使用git pull的时候多次碰见下面的情况: There is no tracking information for the current branch.Please specify whi ...

  3. mysql的时区错误问题: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

    问题:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.. ...

  4. Python——设计模式——单例模式

    一个类始终只有一个实例 当你第一次实例化这个类的时候,就创建一个实例化得对象 当你之后再来实例化的时候,就用之前创建的对象 class A: __instance = False def __ini_ ...

  5. [SimplePlayer] 2. 在屏幕上显示视频图像

    我们这里采用SDL(本文所用版本为SDL2.0.5)来进行图像输出,SDL在进行图像渲染时一般采用的会是direct3D或者opengl,SDL对它们进行了封装,不过我们这里只讨论SDL的使用,并不会 ...

  6. (五)qt资源文件

    // 规则: :+添加的前缀/+文件名 ui->actionSave_as->setIcon(QIcon(":/new/Image/face.png"));

  7. ASP.NET概念

    ASP.NET :是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET两种开发语言:VB C#

  8. SP687 REPEATS - Repeats

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

  9. Go语言系列(三)- 基础函数和流程控制

    一.strings和strconv的使用 1. strings.HasPrefix(s string, prefix string) bool:判断字符串s是否以prefix开头 . 2. strin ...

  10. 基于USB网卡适配器劫持DHCP Server嗅探Windows NTLM Hash密码

    catalogue . DHCP.WPAD工作过程 . python Responder . USB host/client adapter(USB Armory): 包含DHCP Server . ...