poj2796 (单调栈)
来源:互联网 发布:fm数据库 编辑:程序博客网 时间:2024/04/28 04:54
题意:给出一个数组,然后求出区间和与区间最小值 的乘积最大的区间。
思路:可以用线段树之内的来做,不过这里用单调栈来做更加巧妙。有点类似与poj2559,用栈处理出大于它的数的区间最左端和最右端,那么的话这个点就是这个左端和右端的最小值了。区间和的话就用前缀和就行了,很巧妙。
#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<cstring>using namespace std;typedef long long ll;typedef pair<ll,int> P;#define fi first#define se second#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x)#define PI acos(-1.0)#define ITER set<int>::iteratorconst int Mod = 1e9 + 7;const int maxn = 100000 + 10;int a[maxn];int L[maxn],R[maxn];int st[maxn];ll sum[maxn];int main(){ int n; while( ~ scanf("%d",&n)) { sum[0] = 0; for(int i = 1; i <= n;i ++)scanf("%d",&a[i]),sum[i] = sum[i - 1] + a[i]; int top = 0;//从小到大的单调栈 st[0] = 0; for(int i = 1;i <= n; i ++) { int t = -1; while(top && a[st[top]] >= a[i]){t = st[top];top --;} if(t == -1)L[i] = i;else L[i] = st[top] + 1; st[++ top] = i; } top = 0;st[0] = n + 1; for(int i = n;i >= 1; i --) { int t = -1; while(top && a[st[top]] >= a[i]){t = st[top];top --;} if(t == -1)R[i] = i;else R[i] = st[top] - 1; st[++ top] = i;// cout << i << " " << L[i] << " " << R[i] << endl; } ll ans = 0,pos; for(int i = 1; i <= n; i ++) { ll temp = 1ll * a[i] * (sum[R[i]] - sum[L[i] - 1]); if(temp >= ans)ans = temp,pos = i; } printf("%lld\n",ans); printf("%d %d\n",L[pos],R[pos]); } return 0;}
阅读全文
0 0
- poj2796 (单调栈)
- POJ2796 Feel Good(单调栈)
- [poj2796]Feel Good(单调栈)
- POJ2796 Feel Good(单调栈)
- poj2796 Feel Good(单调栈)
- poj2796(继续学习单调栈)
- poj2796 Feel Good(单调栈)
- [POJ2796]Feel Good(单调栈)
- poj2796 Feel Good(单调栈)
- poj2796模拟单调栈
- 单调栈poj2796
- POJ2796->单调栈
- POJ2796【单调栈】
- 单调栈 poj2796
- poj2796 单调栈
- 单调栈 poj2796
- poj2796 Feel Good 单调栈
- 关于单调栈!!poj2796小议
- 汇哲科技CISM每日一题
- C++编程音视频库ffmpeg的pts时间换算方法
- oracle11g scanip 负载均衡测试
- 临渊慕鱼,不如退而结网
- 三个电脑办公小技巧
- poj2796 (单调栈)
- Java基础总结(三)
- the specified jre installation does not exist 不存在指定的JRE安装
- 更改或者新增ssh-key后,需要的操作
- LintCode-[容易] 547. 两数组的交
- 欢迎使用CSDN-markdown编辑器
- arm-linux 开发步骤
- C++处理异常 try,catch,throw
- 【Android】Uri、UriMatcher、ContentUris详解