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

题意:有n个队伍。 对于每个ENQUEUE  x 命令。 如果x所在的队伍已经在队列中, 则x排在队列中它的队伍的尾巴, 否则排在队列的末尾。 可以理解为队列中的队列的味道。

这题有点蒙,但是弄懂后,感觉自己对队列似乎了解深了许多啊!

//Asimple
//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <deque>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <ctype.h>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <time.h>
#include <bitset>
#include <list> using namespace std;
#define INF 0xFFFFFFFF
typedef long long ll ;
typedef list<int>::iterator l_iter;
typedef multimap<string,string>::iterator mss_iter;
typedef map<string,string>::iterator m_iter;
typedef set<int>::iterator s_iter;
typedef vector<int>::iterator v_iter;
multimap<string,string> mss;
const int maxn = 1000;
const int Max = 1000000;
int n, T, num, cnt;
string str;
stack<set<int> > stk;
map<set<int>, int> m;
set<int> s1, s2;
queue<int> q;
queue<int> qq[maxn];
list<int> L;
int a[Max]; int main()
{
int k = 1 ;
while( cin >> T && T )
{
while( !q.empty() ) q.pop();
for(int i=0; i<maxn; i++)
while( !qq[i].empty() )
qq[i].pop();
memset(a,0,sizeof(a));
//入队
for(int i=0; scanf("%d",&n)==1; i++)
{
for(int j=0; j<n; j++)
{
scanf("%d%*c",&num);
a[num] = i ;
}
} cout << "Scenario #" << k << endl ;
while(true)
{
cin >> str ;
if( str == "STOP" )
{
cout << endl ;
break;
}
if( str == "ENQUEUE" )
{
scanf("%d%*c",&num);
if( qq[a[num]].empty() )
q.push(a[num]);
qq[a[num]].push(num);
}
if( str == "DEQUEUE" )
{
int q_num = q.front();
cout << qq[q_num].front() << endl ;
qq[q_num].pop();
if( qq[q_num].empty() )
q.pop();
}
} k ++ ;
} return 0;
}

ACM题目————Team Queue的更多相关文章

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

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

  2. Team Queue(STL练习题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others ...

  3. POJ 2259 - Team Queue - [队列的邻接表]

    题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...

  4. UVA Team Queue

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

  5. UVA.540 Team Queue (队列)

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

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

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

  7. POJ 2259 Team Queue(队列)

    题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...

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

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

  9. UVA540 Team Queue——题解 by hyl天梦

    UVA540 Team Queue 题解 题目描述:题目原题 https://vjudge.net/problem/UVA-540 Queues and Priority Queues are dat ...

随机推荐

  1. Javascript中Number()、parseIn()和parseFloat()的区别

    有3个函数可以把非数值转化成数值:Number().parseInt()和parseFloat().第一个函数,即转型函数Number()可以用于任何数据类型,而另两个函数则专门用于把字符串转换成数值 ...

  2. 手动purge优化器的统计信息与AWR快照,减少对sysaux表空间的占用

    1.运行以下脚本,计算当前优化器统计信息和AWR快照表占用sysaux的空间 SQL> conn / as sysdba SQL> @?/rdbms/admin/awrinfo.sql 2 ...

  3. MVC 学习系列

    总是很难说清MVC的概念,即使读了源代码后(读的时候有些东西,理解起来还是有点吃力),也依然能难对整体的每一个具体的原理说的一清二楚.为了达到自己学习的目的,我把自己的学习路线写成文章,一边自己能对M ...

  4. redhat 6.7 安装nvidia显卡驱动时出现的问题

    一.给Redhat装Nvidia驱动时,出现类似ERROR: The Nouveau kernel driver is currently in use by your system. 的错误,这是应 ...

  5. Swift动画编程指南-01 简介

    大家好,我是老镇,这段时间家里和工作上发生了很多的事情,所以很长一段时间都没有出来搞什么小动作了.在接下来的一段时间内我会制作一些列关于使用Swift进行动画编程的视频,希望和大家胃口. 在iOS的世 ...

  6. ADO.net 扩展属性

    扩展属性 处理:有外键关系时将代号化信息处理成原始文字,让用户可看懂的(粗略解释) 利用扩展属性 如:users表中的民族列显示的是民族代号处理成Nation表中的民族名称 需要在users类里面扩展 ...

  7. [Reprint] C++函数模板与类模板实例解析

    这篇文章主要介绍了C++函数模板与类模板,需要的朋友可以参考下   本文针对C++函数模板与类模板进行了较为详尽的实例解析,有助于帮助读者加深对C++函数模板与类模板的理解.具体内容如下: 泛型编程( ...

  8. 查看真机的系统版本sdk

    1.adb devices 确保连接上了真机 2.adb shell 进入android系统 3.进入system目录下 4.查看build.prop文件 cat build.prop

  9. android命令安装apk时报错:INSTALL_FAILED_CPU_ABI_INCOMPATIBLE

    点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.

  10. bzoj2333 [SCOI2011]棘手的操作

    用set维护每个联通块里的最值,multiset维护所有块里的最值,并查集维护连通性,然后随便搞搞就行了,合并时候采用启发式合并.复杂度O(nlognlogn),大概勉强过的程度,反正跑的很慢就是了. ...