单调栈--poj2796 Feel good
来源:互联网 发布:网络带来的利与弊论文 编辑:程序博客网 时间:2024/05/21 22:54
给定数列,求一个区间内最小值乘区间和,的最大值。
ps:1.最大值可能是0
2.前缀和也是long long
3.手写栈
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespacestd;
const int maxn =1e5 + 5;
typedef longlong ll;
struct node{
int v;
int l,r;//表示v是[l,r]内的最小值
}a[maxn],t;
ll presum[maxn];
node stk[maxn];
int p = 0;
int main()
{
int n;
cin >> n;
for (int i =1; i <= n ; i ++) {
scanf("%d",&a[i].v);
a[i].l =a[i].r = i;//初始化(v,i,i)v至少是[i,i]内的最小值
presum[i] =presum[i - 1] +a[i].v;
}
ll ans = -1,tmp =0;//最大值可能为0
int ans_l =0,ans_r = 0;
stk[p ++] =a[1];
for (int i =2; i <= n; i ++) {
if(a[i].v >=stk[p -1].v)stk[p ++] =a[i];//相等也放上去好了
else {
while (p >0 && stk[p -1].v >a[i].v) {//可知t > top,a[i] > top,t > a[i],用来更新[l,r],并且总是在出栈时更新
t =stk[p -1];p --;
if(p >0){
stk[p -1].r =t.r;
}
a[i].l =t.l;
tmp = t.v * (presum[t.r] - presum[t.l -1]);
if(tmp > ans) {ans = tmp;ans_l =t.l,ans_r =t.r;}
}
stk[p ++] =a[i];
}
}
while (p >0) {//最后要清空栈,最大值也可能出现在这里
t =stk[p -1];p --;
tmp = t.v * (presum[t.r] - presum[t.l -1]);
if(tmp > ans) {ans = tmp;ans_l =t.l,ans_r =t.r;}
if(p >0) stk[p -1].r =t.r;
}
printf("%lld\n",ans);
printf("%d %d\n",ans_l,ans_r);
return0;
}
- 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(单调栈)
- 单调栈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(单调栈求区间问题)
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
- (dp)正整数分组
- java验证邮箱地址是否正确
- JavaScript 产生不重复的随机数三种实现思路
- Linux下开启禁用ping
- 单调栈--poj2796 Feel good
- web App开发中手机滑动的流畅性
- app开发中的异步处理(三)
- Android自定义控件:NestedScrolling实现仿魅族flyme6应用市场应用详情弹出式layout
- Session
- Android开发之利用MQTT协议实现消息的即时推送
- oracle自带表SQL练习(三)
- Android灵魂画家的18种混合模式
- 微信支付开发调试填坑过程记录