ACM-单调队列
对于单调队列的基本概念可以去看百科里的相关介绍:http://baike.baidu.com/view/3771451.htm
这里挑一些重点。
作用:
RMQ即Range Maximum(Minimum) Query,用来求某个区间内的最大值或最小值。使用线段树或稀疏表是O(log(n))级的。对于这类问题这两种方法也搞得定,但是没有单调队列快。
定位:
大多数题目为单调队列力所不能及的,取而代之的是单调队列基础上改进的斜率优化,单调栈等,因为其限制条件,故潜力不大。但需要掌握,因为有许多算法建立在其基础上。
简单说就是基础,得会。
poj 2823
http://acm.hust.edu.cn/vjudge/problem/16694
k大小的滑动窗口,求每次窗口中最大值和最小值
思路:
维护单增队列和单减队列,因为滑动窗口和index有关,所以加个index数组维护head的索引
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1000000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m,k; int a[MAXN],qu[MAXN],index[MAXN],ans[MAXN]; void getMin()
{
int head=,tail=;
for(int i =;i<k;i++)
{
while(head<=tail && a[i]<=qu[tail]) tail--;
qu[++tail] = a[i];
index[tail] = i;
}
for(int i=k;i<=n;i++)
{
while(head<=tail && a[i]<=qu[tail]) tail--;
qu[++tail] = a[i];
index[tail] = i;
while(index[head]<= i-k) head++;
ans[i-k] = qu[head];
}
} void getMax()
{
int head=,tail=;
for(int i =;i<k;i++)
{
while(head<=tail && a[i]>=qu[tail]) tail--;
qu[++tail] = a[i];
index[tail] = i;
}
for(int i =k;i<=n;i++)
{
while(head<=tail && a[i]>=qu[tail]) tail--;
qu[++tail] = a[i];
index[tail] = i;
while(index[head]<= i-k) head++;
ans[i-k] = qu[head];
}
} int main()
{
int i,j,t,kase=;
while(~sf("%d%d",&n,&k))
{
for(i=;i<=n;i++) sf("%d",&a[i]);
getMin();
for(i=;i<=n-k;i++) pf("%d ",ans[i]);
blank;
getMax();
for(i=;i<=n-k;i++) pf("%d ",ans[i]);
blank;
}
return ;
}
ACM-单调队列的更多相关文章
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- 【HDU 3401 Trade】 单调队列优化dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目大意:现在要你去炒股,给你每天的开盘价值,每股买入价值为ap,卖出价值为bp,每天最多买as ...
- HDU 3401 Trade dp+单调队列优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...
- HDU-3401 Trade 单调队列优化DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...
- HDU 4122 Alice's mooncake shop 单调队列优化dp
Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- hdu4374One hundred layer (DP+单调队列)
http://acm.hdu.edu.cn/showproblem.php?pid=4374 去年多校的题 今年才做 不知道这一年都干嘛去了.. DP的思路很好想 dp[i][j] = max(dp[ ...
随机推荐
- MySql数据库内部常用命令大全
1. 连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1) 连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命 ...
- C++期中考试
第一题1. 补足日期类实现,使得能够根据日期获取这是一年中第多少天.(12分) date.h #ifndef DATE_H #define DATE_H class Date { public: Da ...
- 【TED演讲】阿帕玛・饶:(幽默的高科技艺术)
身为艺术家和TED Fellow的阿帕玛・饶对熟悉的事物以惊奇的幽默的方式进行再次想像.通过和索伦・普尔兹的合作,她创作出一系列高科技的艺术作品-一个会发邮件的打字机,一个让你在屏幕上消失而跟踪拍摄你 ...
- docker log: containerid-json.log 文件disappear,问题排查及解决方案
问题排查: 运行 #docker info 查阅资料,知道了docker的logging driver相关理论:https://docs.docker.com/engine/admin/loggi ...
- Eclipse中使用Spring IOC容器的具体方法
1.通过IOC容器创建对象,并为属性赋值 在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建 <bean id=&quo ...
- 2019.2.14 考试T3 交互题
\(\color{#0066ff}{ 题目描述 }\) 由于机房被成功拯救了,花_Q很高兴,花_Q生成了一个 0 到 N - 1 的排列(排列的下标从 0 到 N - 1 ).保证排列中 0 在 N ...
- 「模拟赛20181025」御风剑术 博弈论+DP简单优化
题目描述 Yasuo 和Riven对一排\(n\)个假人开始练习.斩杀第\(i\)个假人会得到\(c_i\)个精粹.双方轮流出招,他们在练习中互相学习,所以他们的剑术越来越强.基于对方上一次斩杀的假人 ...
- NSArray,NSMutable和NSSet,NSMutableSet和NSDictionary,NSMutableDictionary用法
开始编写应用程序的代码时,可以利用大量的 Objective-C 框架.其中,为所有应用程序提供基本服务的 Foundation 框架尤为重要.Foundation 框架包括表示基本数据类型的值类(如 ...
- HashMap 1.8的源码分析二
hashmap的构造方法: public HashMap(int initialCapacity, float loadFactor) { ) throw new IllegalArgumentExc ...
- [转载]np.where()使用说明
转载自https://www.cnblogs.com/massquantity/p/8908859.html#4072620 numpy.where() 有两种用法: 1. np.where(cond ...