51nod 1437 迈克步 单调栈
利用单调栈高效的求出,一个数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 迈克步 单调栈的更多相关文章
- 51nod 1437 迈克步——单调栈
有n只熊.他们站成一排队伍,从左到右依次1到n编号.第i只熊的高度是ai. 一组熊指的队伍中连续的一个子段.组的大小就是熊的数目.而组的力量就是这一组熊中最小的高度. 迈克想知道对于所有的组大小为x( ...
- 51nod 1437 迈克步(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...
- 51nod 1437:迈克步 单调栈基础题
1437 迈克步 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 有n只熊.他们站成一排队伍,从左到右依次1到 ...
- 51nod 1437 迈克步
题目链接 先利用单调栈or其他方法找到一个元素g[i]作为最小值的区间,设为[L, R]. 那么长度为R-L+1的组的最大值ans=max(ans,g[i]).但是有一个问题: 比如6这个元素是长度为 ...
- 51nod1437 迈克步 单调栈
考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...
- 51nod 1102 【单调栈】
思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...
- 51nod 1102 面积最大的矩形 (单调栈)
链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 思路: 首先介绍下单调栈的功能:利用单调栈,可以找到从左/ ...
- 51nod 1102 面积最大的矩形(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 题意: 思路: 做法就是求出每个长方形向左向右所能延伸的最大距离. ...
- 51nod 1215 单调栈/迭代
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 数组的宽度 题目来源: Javaman 基准时间限制:1 ...
随机推荐
- centos 7 安装appache 服务器
一.安装Apache程序,一般有三种安装方式:1.直接网络安装:2.下载rpm包,上传至服务器进行安装:3.通过原代码编译安装: yum -y install httpd rpm -qa | grep ...
- (栈)leetcode856 Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- Linux 中用 dd 命令来测试硬盘读写速度
dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. dd 命令通用语法格式如下: dd if=path/to/input_file ...
- scrapy 基础
安装略过 创建一个项目 scrapy startproject MySpider #或者创建时存储日志scrapy startproject --logfile='../logf.log' MySpi ...
- hadoop 2.5 安装部署
hadoop 下载地址:http://mirrors.cnnic.cn/apache/hadoop/common/ 单机伪分布 配置文件: /hadoop-2.5.1/etc/hadoop/ hado ...
- WebService概念和使用
1.WebService到底是什么 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用 ...
- Quartus II 中 Verilog 常见警告/错误汇总
Verilog 常见错误汇总 1.Found clock-sensitive change during active clock edge at time <time> on regis ...
- Django之组件--auth组件
目录 Auth模块是什么 auth模块常用方法 扩展默认的auth_user表 1 Django自带的用户认证模块,可以快速的实现登录,注销,修改密码... 2 扩展auth表,需要继承Abstrac ...
- Sublime Text 3 浅色主题
1.参考 如何优雅使用Sublime Text3(Sublime设置豆沙绿背景色和自定义主题) Fluidvision Inspiredgithub LightRays Mac 20Classic P ...
- Arrays.asList() 的使用注意
Sometimes it is needed to convert a Java array to List or Collection because the latter is a more po ...