Team Queue UVA - 540
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的更多相关文章
- uva 540 (Team Queue UVA - 540)
又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- UVA 540 Team Queue(模拟+队列)
题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...
- 【UVA - 540】Team Queue (map,队列)
Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...
- UVA Team Queue
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013840081/article/details/26180081 题目例如以下: Team Qu ...
- hdu 1387(Team Queue) STL
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Team Queue (uva540 队列模拟)
Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...
- UVA 540 stl
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
随机推荐
- Laravel Queues 队列应用实战
队列,顾名思义,排着队等着做事情.在生活场景中,凡是排队的人,都是带有目的性的.要完成某件事情,才去排队的,要不没有谁会闲到排队玩儿.而在软件应用层面,队列是什么,队列有什么优点,我们什么时候需要用队 ...
- 由endl对printf和cout的思考
[前言]二者的区别就不介绍了.二者使用方法: printf("%s",a); cout<<a<<endl; endl的作用是什么? 一.endl作用 众所周 ...
- nacos服务注册之服务器端Distro
一致性协议算法Distro阿里自己的创的算法吧,网上能找到的资料很少.Distro用于处理ephemeral类型数据 Distro协议算法看代码大体流程是: nacos启动首先从其他远程节点同步全部数 ...
- MySQL:字段约束与索引
字段约束 MySQL的字段约束共四种: 约束名 关键字 描述 主键约束 PRIMARY KEY 字段值唯一,且不能为NULL 非空约束 NOT NULL 字段值不能为NULL 唯一约束 UNIQUE ...
- MySQL注入点与SQL语句的关系
目录 注入位置分类 内联式 - UNION query SQL injection 终止式 - End SQL injection 堆叠式 - Stacked queries SQL injectio ...
- QuickBase64 - Android 下拉通知栏快捷base64加解密工具
Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...
- POJ-1797(最短路变形-dijkstra)
Heavy Transportation POJ-1797 这题是最短路题型的变形,该题不是求起点到终点的最短路,而是求路径中的最小边的最大值. 这题的求解思路是:将原来dijkstra中的松弛方程改 ...
- OpenGL光照贴图
一:啥叫贴图 上一节中,我们将整个物体的材质定义为一个整体,但现实世界中的物体通常并不只包含有一种材质,而是由多种材质所组成. 拓展之前的系统,引入漫反射和镜面光贴图(Map).这允许我们对物体的漫反 ...
- java将一个list转换成一个String,中间用分隔符隔开
List sn=[123,1231,1231,231] sn.toString();//[123,1231,1231,231] sn.join(',').toString();//123,1231,1 ...
- arcgis for js 4.6加载本地发布好的2维地图
我本地发布好的地图服务信息如下图所示: 我们在代码中使用到的url是图中所示的REST URL 加载代码如下: <!DOCTYPE html> <html> <head& ...