单调栈1001 POJ2796 Feel Good
来源:互联网 发布:流程的再造与优化 编辑:程序博客网 时间:2024/04/30 18:01
题意:
给一个序列代表某个人的第i天的幸福指数,求一个最幸福的时间段
时间段的幸福指数是该时间段幸福指数的和*该时间段内最低的幸福指数
思路:
求出以第i天为最低的幸福指数的最大区间
单调栈的经典问题
注意一下下边的数据…
InPut:
1
0
OutPut:
0
1 1
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include<string>#include<vector>#include<map>#include<set>using namespace std;#define lowbit(x) (x&(-x))typedef long long LL;const int maxn = 100005;const int inf=(1<<28)-1;stack<int>Stack;int A[maxn],Right[maxn],Left[maxn];LL Sum[maxn];int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&A[i]); Sum[i]=Sum[i-1]+A[i]; } for(int i=1;i<=n;++i) { while(!Stack.empty()&&A[i]<A[Stack.top()]) { Right[Stack.top()]=i-1; Stack.pop(); } Stack.push(i); } while(!Stack.empty()) { Right[Stack.top()]=n; Stack.pop(); } for(int i=n;i>=1;--i) { while(!Stack.empty()&&A[i]<A[Stack.top()]) { Left[Stack.top()]=i+1; Stack.pop(); } Stack.push(i); } while(!Stack.empty()) { Left[Stack.top()]=1; Stack.pop(); } LL Ans=-1; int LeftPoint=-1,RightPoint=-1; for(int i=1;i<=n;++i) if(Ans<(Sum[Right[i]]-Sum[Left[i]-1])*A[i]) { Ans=(Sum[Right[i]]-Sum[Left[i]-1])*A[i]; LeftPoint=Left[i]; RightPoint=Right[i]; } printf("%lld\n%d %d\n",Ans,LeftPoint,RightPoint); return 0;}
0 0
- 单调栈1001 POJ2796 Feel Good
- poj2796 Feel Good 单调栈
- POJ2796 Feel Good(单调栈)
- poj2796 Feel Good 单调栈
- poj2796 Feel Good 单调栈
- POJ2796 Feel Good【单调栈】
- 单调栈--poj2796 Feel good
- POJ2796 Feel Good 单调栈
- POJ2796 Feel Good(单调栈)
- [poj2796]Feel Good(单调栈)
- POJ2796 Feel Good(单调栈)
- poj2796 Feel Good(单调栈)
- poj2796 feel good 单调栈 前后延伸
- poj2796 Feel Good(单调栈)
- [POJ2796]Feel Good(单调栈)
- poj2796 Feel Good(单调栈)
- POJ2796 Feel Good -- 单调队列
- poj2796--Feel Good(单调栈求区间问题)
- for循环语句
- OFDM消除ICI和ISI的原理剖析
- MVC设计模式
- office文件图标显示不正常
- Activity设置成Dialog模式
- 单调栈1001 POJ2796 Feel Good
- ListView嵌入ScrollView问题解决大全
- Minimal Steiner Tree(MST)
- 【NOIP2016提高A组模拟7.17】锦标赛
- 拉塞尔·威斯布鲁克:不知疲倦的“铁人”
- 单调栈1002 POJ 2559 Largest Rectangle in a Histogram
- 正则表达式_鸟叔学习笔记
- linux查看文件/目录 大小(du)、权限(ls ll),用户及组(user/group)增、删、改 (chmod/chown/chgrp)
- 数据结构(8) 构造数组的MaxTree