【UVA - 540】Team Queue (map,队列)
Team Queue
Descriptions:
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
先说一下题目要求
有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友身后。如果没有任何一个队友排队,则他会排到长队的队尾。输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)。
ENQUEUE:编号为X的人进入长队。
DEQUEUE:长队队首出队。
STOP:停止模拟。
对于每个DEQUEUE指令,输出出队的人的编号。
输入
输入文件中有一组或多组测试数据。
每组测试数据开始有t个团队。下面t行,每行的第一个数字代表这个团队人数,后面是这几个人的编号。编号为0到999999之间的一个整数。
每组测试数据以“STOP”结束。
输入以t==0时结束。
警告:一个测试用例可能包含最多200000(二十万)个命令,所以实现
团队的队列应该是有效的。
输出
对于每组测试数据,先打印一句"Scenario #k",k是第几组数据。对于每一个"DEQUEUE"指令,输出一个出队的人的编号。每组测试数据后要换行,即使是最后一组测试数据。
题目链接:
https://vjudge.net/problem/UVA-540
先用map找到每个人员所对应的团队,再用队列queue去分别存放团队的编号和人员的编号
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int main()
{
int n;
int sum=;
while(cin>>n,n)
{
sum++;
cout<<"Scenario #"<<sum<<endl;
map<int,int> team;//一个人员编号对应一个团队
for(int i=; i<n; i++)
{
int x;
cin>>x;
for(int j=; j<x; j++)
{
int num;
cin>>num;
team[num]=i;//人员编号对应他的团队序号
}
}
string s;
queue<int> big;//团队的队列,代表有几个团队
queue<int> small[];//每个团队人员的队列
while(cin>>s&&s!="STOP")
{
if(s=="ENQUEUE")
{
int num;
cin>>num;
int t=team[num];//这到这个人所对应的团队
if(small[t].empty())//如果找不到团队,则重新建立一个团队
big.push(t);
small[t].push(num);//把这个人让如这个团队
}
if(s=="DEQUEUE")
{
int t=big.front();//取出第一个团队
if(!small[t].empty())//团队不为空,则去取这个团队的人员
{
cout<<small[t].front()<<endl;
small[t].pop();
}
if(small[t].empty())
big.pop();
}
}
cout<<endl;
}
}
【UVA - 540】Team Queue (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 【STL】
题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...
- UVA 540 Team Queue
思路:使用优先队列,按队伍出现的时刻和自身出现的时刻定义优先级,同时记录此时刻队列里是否有自己队伍的人,一开始没注意,wa了两发. #include<map> #include<qu ...
- uva 540 (Team Queue UVA - 540)
又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
Description Team Queue Team Queue Queues and Priority Queues are data structures which are know ...
- Team Queue (uva540 队列模拟)
Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...
- UVa540 Team Queue(队列queue)
队列 STL队列定义在头文件<queue>中, 用“ queue<int>s ” 方式定义, 用push()和pop()进行元素的入队和出队操作, front()取队首元素(但 ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
随机推荐
- Git多账号登陆
最近工作上遇到了使用git+repo的情况,需要用公司的邮箱和账号名重新申请ssh公私密钥,而我本身在github上也有一些开源项目,这里就是记录一下我是如何实现git多账号登陆的. 取消 ...
- FLEX接收外部参数 .
FLEX参数传递与FLASH有点不同 login..swf?name=aa&password=bb Flex上是这样接收参数的 myname=mx.core.Application.appli ...
- hdu3076ssworld VS DDD 概率dp
//ssworld VS DDD 两个人有血量值 hp1 , hp2 //两人掷骰子得到每一点的概率已知 //ssword赢的概率 //dp[i][j] 表示有第一个人血量为i.第二个人的血量为j ...
- android studio 非法字符: '\ufeff' 解决方式
今天发现一个问题,就是从其它地方拷贝的代码到AS项目里面,木有语法 错误,可是就是执行不起来,老是报错"非法字符: '\ufeff' ",郁闷非常久.木有看到这个字符.最后查询了这 ...
- Arcgis Engine(ae)接口详解(7):地图(map)操作
IMap map = null; //跟map同一层次的activeView对象,他们都是“地图”的对象,map管理地图内容,activeView管理显示内容 IActiveView activeVi ...
- C语言的一些特殊使用方法————————【Badboy】
一:特殊的字符串宏 [cpp] #define A(x) T_##x #define B(x) #@x #define C(x) #x 我们如果x=1, 则上面的宏定义会被解释成下面的样子 A(1)- ...
- Pattern: Microservice Architecture
Microservice Architecture pattern http://microservices.io/patterns/microservices.html Context You ar ...
- c3p0+spring
1. 首先是jdbc.properties属性文件的编写,便于数据库移植: datasource.driverClassName=oracle.jdbc.driver.OracleDriverdata ...
- 「翻译」Unity中的AssetBundle详解(一)
AssetBundles AssetBundle是一个存档文件,其中包含平台在运行时加载的特定资产(模型,纹理,预制,音频剪辑,甚至整个场景).AssetBundles可以表示彼此之间的依赖关系;例如 ...
- HDU3416 Marriage Match IV —— 最短路径 + 最大流
题目链接:https://vjudge.net/problem/HDU-3416 Marriage Match IV Time Limit: 2000/1000 MS (Java/Others) ...