UVA 540 Team Queue(模拟+队列)
题目代号:UVA 540
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=481
题目描述:
Team Queue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2645 Accepted Submission(s): 910
Problem Description
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 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.
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排在队列中它的队伍的尾巴, 否则排在队列的末尾。 可以理解为队列中的队列的味道。对于DEQUEUE命令输出总队列中第一个小组的第一个元素。对于STOP命令,代表停止命令的输入。
解题思路:代码中的注释有详细描述。
AC代码:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; char str[];
int team[];//最大编号不会超过999999所以用这个来表示编号所属的小组号 int main()
{
int n,t,num,cases=;
while(scanf("%d",&t),t)
{
mem(team,);
queue<int>Q[];
queue<int>que;//这样定义在while内就不需要通过循环清空队列,循环一次队列就初始化一次
for(int i=; i<=t; i++)
{
scanf("%d",&n);
for(int j=; j<=n; j++)
{
scanf("%d",&num);
team[num]=i;//初始化该编号成员所属小组
}
}
printf("Scenario #%d\n",cases++);
do
{
scanf("%s",str);
if(!strcmp(str,"ENQUEUE"))
{
scanf("%d",&num);
if(Q[team[num]].empty())que.push(team[num]);//如果该编号成员所属小组的队列没有人,则在总队列尾部添加该小组
Q[team[num]].push(num);//小组所属的队列尾部添加该成员
}
else if(!strcmp(str,"DEQUEUE"))
{
printf("%d\n",Q[que.front()].front());//输出总队列中首部的小组所对应的第一个成员
Q[que.front()].pop();//删除已经输出编号的成员
if(Q[que.front()].empty())//如果该小组人已经没人了,则在总队列中删除这个小组,之后的循环是对下一个小组进行操作
que.pop();
}
}
while(strcmp(str,"STOP"));
cout<<endl;
}
return ;
}
UVA 540 Team Queue(模拟+队列)的更多相关文章
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- 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 UVA - 540)
又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...
- UVA 540 Team Queue
思路:使用优先队列,按队伍出现的时刻和自身出现的时刻定义优先级,同时记录此时刻队列里是否有自己队伍的人,一开始没注意,wa了两发. #include<map> #include<qu ...
- Team Queue (uva540 队列模拟)
Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
Description Team Queue Team Queue Queues and Priority Queues are data structures which are know ...
- UVa540 Team Queue(队列queue)
队列 STL队列定义在头文件<queue>中, 用“ queue<int>s ” 方式定义, 用push()和pop()进行元素的入队和出队操作, front()取队首元素(但 ...
随机推荐
- Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.2 Applications of Propositional Logic
Translating English Sentences System Specifications Boolean Searches Logic Puzzles Logic Circuits
- 【Linux开发】【Qt开发】tslibs的配置(触摸屏没有,HDMI屏幕):Qt界面响应USB鼠标
s3c2416 linux qt4.x 由于触摸屏坏了,板子只能用鼠标了,结果以前可以用的现在鼠标突然不能用了 为此交叉编译了qt的多个版本,也换过根文件系统,以为是tslib版本的问题,却发现q ...
- django 的 三个 时间 字段
- Redis-集群操作
一.查看集群状态 1.查看集群状态 /opt/redis/src/redis-cli -h 本机IP -p redsi实例端口 -c #连接redis实例 /opt/redis/src/redis-c ...
- Ubuntu 16.04简单配置备忘录
1.几个安装包的地址 1.Linux QQ:https://im.qq.com/linuxqq/index.html 2.网易云音乐:http://s1.music.126.net/download/ ...
- npm errno -4048错误
这种错误是缓存原因导致的,首先清除缓存 npm cache clean --force 然后校验缓存依赖的完整和有效性 npm cache verify 最后重新安装即可
- cqoj921E整数匹配
这是一个贪心题,把我坑的好惨,忘还原得70.上午被卡得,, 首先给出长度为n的一组数,可以两两配对相乘也可以进行相加,问怎样才可以使总和最大?那么可以显然看出来,当这个数为0或1时,我们要相加.其余进 ...
- Luogu P5444 [APIO2019]奇怪装置
题目 这种题目看上去就是有循环节的对吧. 在考场上,一个可行的方式是打表. 现在我们手推一下这个循环节. 记函数\(f(t)=(((t+\lfloor\frac tB\rfloor)\%A),(t\% ...
- lambda map() filter() zip()练习
练习: 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb l=[{'name':'alex'},{'name':'y'}] l=[{'name':'alex'},{'name': ...
- HNUSTOJ-1258 Time
1258: Time 时间限制: 1 Sec 内存限制: 128 MB提交: 16 解决: 11[提交][状态][讨论版] 题目描述 Digital clock use 4 digits to e ...