题目代号: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(模拟+队列)的更多相关文章

  1. UVA.540 Team Queue (队列)

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

  2. uva 540 - Team Queue(插队队列)

    首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...

  3. UVa 540 Team Queue 【STL】

    题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...

  4. uva 540 (Team Queue UVA - 540)

    又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...

  5. UVA 540 Team Queue

    思路:使用优先队列,按队伍出现的时刻和自身出现的时刻定义优先级,同时记录此时刻队列里是否有自己队伍的人,一开始没注意,wa了两发. #include<map> #include<qu ...

  6. Team Queue (uva540 队列模拟)

    Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...

  7. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

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

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

  9. UVa540 Team Queue(队列queue)

    队列 STL队列定义在头文件<queue>中, 用“ queue<int>s ” 方式定义, 用push()和pop()进行元素的入队和出队操作, front()取队首元素(但 ...

随机推荐

  1. vim加脚本注释和文本加密

    vim /etc/vimrc 一.李导版本 autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()" fun ...

  2. Python示例-Logging

    logging.ini日志配置文件内容示例: [loggers] keys=root,demo [handlers] keys=consoleHandler,timedRotatingFileHand ...

  3. CentOS安装部署sha##dow**socks

    注意事项,pip版本不能太低,实测9.0.3可行(需要python 2.7,低版本python升级办法另有文章介绍). pip --version 以shadowsocks-2.8.2为例: pip ...

  4. jenkins初级使用篇

    1.jenkins的初级使用 1.1 介绍 创建一个项目 可以看到当前登陆用户及用户权限 可以查看到所有构建过的项目的历史 系统管理 My Views:视图功能,我们可以自己创建一个自己的视图 系统管 ...

  5. centos7安装nginx并配置前端环境

    服务器环境:CentOS Linux release 7.5.1804 (Core) 安装路径:/usr/local 1.安装编译工具以及库文件 (新服务器,未安装则需要先安装) # yum -y i ...

  6. Dos - 学习总结(1)

    1.控制台复制 1>鼠标右键,标记. 2>选定复制内容后,鼠标右键,完成复制. 2.

  7. 为什么你的javascript学了这么久,水平还是烂成了渣?

    今年我给公司面试时,面试了百来个人,水平我就呵呵了,还觉得自己学了很久很厉害了,其实呢,渣的很呀,这篇文章送给想学好javascript找份工作的同学们. 首先要说明的是,咱现在不是高手,最多还是一个 ...

  8. 最长公共子序列(LCS) Medium1

    In a few months the European Currency Union will become a reality. However, to join the club, the Ma ...

  9. C++的同名属性(没有虚拟属性)、同名普通函数、同名静态函数(没有虚拟静态函数),是否被覆盖

    例子1:属性的覆盖#include "stdafx.h" class A {public: int i; A() { i=1; }}; class B: public A {pub ...

  10. TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good

    TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...