poj 2559 单调栈
来源:互联网 发布:多线程编程 java 编辑:程序博客网 时间:2024/05/16 10:30
题目大意:求每个值以它为最小值(可以等于)的最大区间长度
思路:用单调栈维护一个单调不增的序列
这里将一遍正向循环的做法:
首先:栈若非空,且此时栈顶元素大于当前元素:那么由于当前元素比栈顶元素小,那么当前元素的左边界至少为栈顶元素的左边界,而对于栈顶元素来说,当前元素比栈顶元素本身小,那么栈顶元素的右边界则为当前位置-1
Code:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fod(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=1e6+10;typedef long long ll;int a[N],n,l[N],r[N],stack[N],top;int main() { while(scanf("%d",&n)&&n) { fo(i,1,n) scanf("%d",&a[i]); a[++n]=-1; top=0; for(int i=1; i<=n; ++i){ l[i]=r[i]=i; while(top && a[stack[top]]>a[i]){ l[i]=l[stack[top]]; r[stack[top]]=i-1; --top; } if(top && a[stack[top]]==a[i]) l[i]=l[stack[top]]; stack[++top]=i; } ll ans=-1; for(int i=1;i<n;i++) ans=max(ans,(r[i]-l[i]+1LL)*a[i]); cout<<ans<<endl; } return 0;}
阅读全文
0 0
- POJ 2559 单调栈
- POJ 2559 单调栈
- poj 2559(单调栈)
- poj 2559 单调栈
- POJ 2559 单调栈
- poj 2559 单调栈
- poj 2559 单调栈
- POJ 2559 单调栈 Histogram
- poj 2559(单调栈)
- 单调栈(poj -- 2559)
- POJ 2559 单调栈模板
- POJ 2559 单调栈模板题
- POJ - 2559(单调栈入门题)
- 【单调栈】POJ 3250
- poj 2796#单调栈
- POJ 2796 单调栈~
- poj 2796(单调栈)
- poj 2059 单调栈
- 拓展欧几里得 [Noi2002]Savage
- 集训第十六天(2017/8/15):树状数组
- LYOS —— Driver for Keyboard & Mouse
- 几种常用的传感器(加速度传感器、重力传感器、方向传感器、陀螺仪)简介
- 【ZJOI2008】骑士
- poj 2559 单调栈
- Ubuntu 安装Phpstorm+汉化+激活
- 回环变位(Circular Rotation)
- 定位与层级
- mybatis 查询
- 构建Sql多条件查询语句
- 『洛谷T7835』士兵站队问题
- 【JZOJ 5272】神奇的重复序列
- Python的PIL库实现验证码图片