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()取队首元素(但 ...
随机推荐
- Lesson 5 The facts
go to extremes走极端 provide... with...向..提供.. go to press付印 suspicious,adj. 可疑的:怀疑的:多疑的 fired---同义词--- ...
- 20191127 Spring Boot官方文档学习(4.25)
4.25. Testing Spring Boot提供了许多实用程序和注解,可以在测试应用程序时提供帮助.测试支持由两个模块提供:spring-boot-test包含核心项,spring-boot-t ...
- Dapper基本使用
http://www.cnblogs.com/Sinte-Beuve/p/4231053.html
- 首次全备及事务备份对数据库的影响,2014 SpexSql log评估版探索
参考:https://www.cnblogs.com/gered/p/9882367.html 关键词:解析事务日志 新建数据库test3,然后查看日志文件,382行记录 SELECT min([Be ...
- r子集代码实现(递归)
#!/usr/bin/env python #coding:utf-8 SET_START = 1 SET_END = 9 SUB_LEN = 10 def r_subset(i, r, pre, a ...
- AcWing 875. 快速幂
题目链接:https://www.acwing.com/problem/content/description/877/ 快速幂模板题,计算ab mod p 的值,a,b,p大概1e9左右,可以快速计 ...
- c++ 判断两圆位置关系
对于两圆的位置一般有五种关系: (1) 外离:两圆的半径之和小于两圆圆心距离 (2) 外切:两圆的半径之和等于两圆圆心距离 (3) 相交:两圆的半径之和大于两圆圆心距离,两圆圆心距离大于两圆半径之差 ...
- JS的for循环包裹异步函数的问题
有个循环,循环一个异步回调,为啥回调引用的循环值都是最后一步循环的循环值?然后,又有些时候无论什么循环值都得不到? var arr = [1,3,5,7,9]; var arrLength = arr ...
- 基于ELK进行邮箱访问日志的分析
公司希望能够搭建自己的日志分析系统.现在基于ELK的技术分析日志的公司越来越多,在此也记录一下我利用ELK搭建的日志分析系统. 系统搭建 系统主要是基于elasticsearch+logstash+f ...
- Vue框架前言
Vue框架 Vue 框架: 官网 vue框架:渐进式JavaScript框架 vue一个环境:可以只控制页面中一个标签.可以控制一组标签.可以控制整个页面.可以控制整个项目 vue可以根据实际需求,选 ...