利用单调栈高效的求出,一个数a[i]在哪个区间内可作为最小值存在。

正向扫描,求出a[i]可做为最小值的区间的左边界

反向扫描,求出a[i]可作为最小值的区间的右边界

r[i] - l[i] +1 就是a[i]可作为最小值的区间的 最大长度

我们知道:长度为len的区间,包含长度为len-1的区间

所以最后,需要逆向[确保无后效性]更新答案数组,保留最大值。

#include<stdio.h>
#include<math.h>
#include<cstring>
#include<stack>
#include<iostream>
#include<algorithm>
#include<queue>
#define MAXSIZE 200005
#define LL long long using namespace std;
const int INF=; int a[MAXSIZE],l[MAXSIZE],r[MAXSIZE],s[MAXSIZE],dp[MAXSIZE]; int main()
{
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
int n,top;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
top = ;
for(int i=;i<=n;i++) //递增栈
{
if(top==)
{
s[++top] = i;
l[i] = i;
} else
{
while(top>= && a[s[top]]>=a[i])
{
top--;
}
if(top==)
l[i] = ;
else
l[i] = s[top]+;
s[++top] = i;
}
} top = ;
for(int i=n;i>=;i--)
{
if(top==)
{
s[++top] = i;
r[i] = i;
} else
{
while(top>= && a[s[top]]>=a[i])
{
top--;
}
if(top==)
r[i] = n;
else
r[i] = s[top]-;
s[++top] = i;
}
} for(int i=;i<=n;i++)
{
dp[r[i]-l[i]+] = max(dp[r[i]-l[i]+],a[i]);
} for(int i=n-;i>=;i--)
{
dp[i] = max(dp[i+],dp[i]);
} for(int i=;i<=n;i++)
{
printf("%d%c",dp[i],i==n?'\n':' ');
}
return ;
}

51nod 1437 迈克步 单调栈的更多相关文章

  1. 51nod 1437 迈克步——单调栈

    有n只熊.他们站成一排队伍,从左到右依次1到n编号.第i只熊的高度是ai. 一组熊指的队伍中连续的一个子段.组的大小就是熊的数目.而组的力量就是这一组熊中最小的高度. 迈克想知道对于所有的组大小为x( ...

  2. 51nod 1437 迈克步(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...

  3. 51nod 1437:迈克步 单调栈基础题

    1437 迈克步 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 有n只熊.他们站成一排队伍,从左到右依次1到 ...

  4. 51nod 1437 迈克步

    题目链接 先利用单调栈or其他方法找到一个元素g[i]作为最小值的区间,设为[L, R]. 那么长度为R-L+1的组的最大值ans=max(ans,g[i]).但是有一个问题: 比如6这个元素是长度为 ...

  5. 51nod1437 迈克步 单调栈

    考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...

  6. 51nod 1102 【单调栈】

    思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...

  7. 51nod 1102 面积最大的矩形 (单调栈)

    链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 思路: 首先介绍下单调栈的功能:利用单调栈,可以找到从左/ ...

  8. 51nod 1102 面积最大的矩形(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 题意: 思路: 做法就是求出每个长方形向左向右所能延伸的最大距离. ...

  9. 51nod 1215 单调栈/迭代

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 数组的宽度 题目来源: Javaman 基准时间限制:1 ...

随机推荐

  1. TODO java 作业-梭哈--待完成

    作业:定义一个类,该类用于封装一桌梭哈游戏,这个类应该包含桌上剩下的牌的信息,并包含5个玩家的状态的信息,他们各自的位置,游戏状态(正在游戏或已放弃),手上已有的牌等信息.如果有可能,这个类还应该实现 ...

  2. (链表) leetcode 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  3. bzoj3991 LCA + set

    https://www.lydsy.com/JudgeOnline/problem.php?id=3991 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有 ...

  4. JSP学习记录

    <%@ page contentType="text/html;charset=gb2312" %> <html> <h1>计算器</h1 ...

  5. 2017-12-15python全栈9期第二天第三节之作业讲解用户三次登陆

    #!/user/bin/python# -*- coding:utf-8 -*-i = 0while i < 3: username = input('请输入账号:') password = i ...

  6. 2017-12-14python全栈9期第一天第五节之变量、常量、注释

    6,变量. 变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用. 1,必须由数字,字母,下划线任意组合,且不能数字开头. 2,不能是python中的关键字. ['and', 'as', 'a ...

  7. Python的常用语句

    判断语句 if单层条件判断 if expression: statements1 else: statements2 if多层条件判断 if expression1: statements1 elif ...

  8. Hbuilder开发app时生成ios要的mobileprovision和p12文件步骤.

    1.在MAC电脑.钥匙串串访问->证书助理->从证书颁发机构请求证书,创建一个证书为certSigningRequest文件 2.在Apple Developer中的Certificate ...

  9. Trailing slash

    Trailing Slash common case It's common for URLs with a trailing slash to indicate a directory, and t ...

  10. Debugger for chrome

    Debugger In VScode Getting Started Install the extension Debugger for chrome Config the launch.json ...