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

  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

HINT

这个题目不用紫皮书上的方法好像没有啥简单点的方法了。用的双队列,因为题目的意思是每一个排队成员都有属于自己的队伍,因此没进来一个人都会去找到自己的团队里面的最后一个的后面排队。因此用队列数组存储每一个小团队的队列,然后再加一个队列来存储各个团队的队列。更具体的壳代码和紫皮书就好。

Accepted

#include<algorithm>
#include <iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<stack>
#include<queue> using namespace std; int main()
{
int m,id, n,num=1;
while (1){
map<int, int>teams; //记录组名;
cin >> m;
if (!m)break;
for (int i = 0;i < m;i++){
cin >> n;
for (int j = 0;j < n;j++){
cin >> id;
teams[id] = i;
}
}
queue<int>q1, q2[1010];
string s;
cout << "Scenario #" << num++ << endl;
while (cin >> s && s != "STOP")
{
if (s == "DEQUEUE"){
cout << q2[q1.front()].front() << endl;
q2[q1.front()].pop();
if (q2[q1.front()].empty())q1.pop();
}
else {
cin >> id;
if (q2[teams[id]].empty())q1.push(teams[id]);
q2[teams[id]].push(id);
}
}
cout << endl;
}
}

Team Queue UVA - 540的更多相关文章

  1. uva 540 (Team Queue UVA - 540)

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

  2. UVA.540 Team Queue (队列)

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

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

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

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

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

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

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

  6. UVA Team Queue

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013840081/article/details/26180081 题目例如以下: Team Qu ...

  7. hdu 1387(Team Queue) STL

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

  8. Team Queue (uva540 队列模拟)

    Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...

  9. UVA 540 stl

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

随机推荐

  1. 1086 Tree Traversals Again——PAT甲级真题

    1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...

  2. COM技术中的VARIANT and VARIANTARG

    VARIANT and VARIANTARG Use VARIANTARG to describe arguments passed within DISPPARAMS, and VARIANT to ...

  3. 企业安全_检测SQL注入的一些方式探讨

    目录 寻找SQL注入点的 way MySQL Inject 入门案例 自动化审计的尝试之旅 人工审计才能保证精度 寻找SQL注入点的 way 在企业中有如下几种方式可以选择: 自动化 - 白盒基于源码 ...

  4. macOS下Chrome和Safari导入证实抓包HTTPS

    目录 下载证书 mac OS导入证书 Chrome设置代理 Safari设置代理 下面的操作基于Mac OS Catalina(v10.15.3),抓包拦截工具基于Burp Suite v2.1.05 ...

  5. HDOJ-1754(线段树+单点更新)

    I Hate It HDOJ-1754 这道题是线段树简单的入门题,只是简单考察了线段树的基本使用,建树等操作. 这里需要注意的是输入要不使用scanf要不使用快速输入. 这里的maxs数组需要开大一 ...

  6. Sass/Scss 基础篇

    Sass/Scss 基础篇 总结Sass学习到的内容 应用Sass/Scss前,环境配置 首先下载Ruby (http://rubyinstaller.org/downloads) 通过命令下载sas ...

  7. 强制断开ssh连接出现ssh崩溃问题

    出现原因 finalshell意外终止,导致ssh连接意外终止 之后怎么都连不上虚拟机的ssh,一看是虚拟机的ssh已经被意外暂停,可能是跟finalshell的意外终止有关 解决 chmod 600 ...

  8. 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID

    问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...

  9. 修饰符static和abstract

    修饰符static和abstract static static可以修饰类中的方法,属性等,被修饰后的方法和属性可以通过类名直接调用也可以通过对象调用.普通的变量只能通过对象进行调用. 静态方法直接可 ...

  10. 20个最有用的Python数据科学库

    核心库与统计 1. NumPy(提交:17911,贡献者:641) 一般我们会将科学领域的库作为清单打头,NumPy 是该领域的主要软件库之一.它旨在处理大型的多维数组和矩阵,并提供了很多高级的数学函 ...