单调栈求每个数在哪些区间是最值的经典操作。

  把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间。

  poj2796 弹出的时候更新答案即可

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
int n,top,ansl,ansr;
ll ans=-;
int st[maxn],a[maxn];
ll sum[maxn];
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
int main()
{
read(n);
for(int i=;i<=n;i++)read(a[i]),sum[i]=sum[i-]+a[i];a[++n]=-;
for(int i=;i<=n;i++)
{
for(;top&&a[i]<=a[st[top]];top--)
{
ll val=1ll*a[st[top]]*(sum[i-]-sum[st[top-]]);
if(val>ans)ans=val,ansl=st[top-]+,ansr=i-;
}
st[++top]=i;
}
printf("%lld\n%d %d",ans,ansl,ansr);
return ;
}

  51nod1215 求出最大值对答案的贡献之和与最小值对答案的贡献之和相减即可。

  显然贡献为a[i]*(i-st[top])*(st1[top]-st[top-1]);

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
int n,top1,top2;
int a[maxn],b[maxn],st1[maxn],st2[maxn];
ll mx,mn;
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
int main()
{
read(n);
for(int i=;i<=n;i++)read(a[i]),b[i]=a[i];b[++n]=inf;
for(int i=;i<=n;i++)
{
for(;top1&&a[i]<=a[st1[top1]];top1--)
mn+=1ll*a[st1[top1]]*(i-st1[top1])*(st1[top1]-st1[top1-]);
for(;top2&&b[i]>=b[st2[top2]];top2--)
mx+=1ll*b[st2[top2]]*(i-st2[top2])*(st2[top2]-st2[top2-]);
st1[++top1]=st2[++top2]=i;
}
printf("%lld\n",mx-mn);
return ;
}

51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)的更多相关文章

  1. 51nod 1215 数组的宽度(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 题意: 思路: 计算出以第i个数为最大值的区间范围,l_max[i ...

  2. 51nod 1215 数组的宽度

    若一个数在一段区间内作为最大值存在,那么答案应该加上这个数 若一个数在一段区间内作为最小值存在,那么答案应该减去这个数 所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定, ...

  3. poj 2796 Feel Good单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case T ...

  4. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  5. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  6. 差异:后缀数组(wzz模板理解),单调栈

    因为涉及到对模板的理解,所以就着代码看会好一些. 让那些坚决不颓代码的人受委屈了. 我是对着wzz的板子默写的,可能不完全一样啊. 还有代码注释里都是我个人的理解,不保证正确,但欢迎指正. 可以有选择 ...

  7. poj 2796 Feel Good 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8753   Accepted: 2367 Case Ti ...

  8. POJ - 2796 Feel Good 单调递增栈+前缀和

    Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...

  9. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

随机推荐

  1. 「Python」Convert map object to numpy array in python 3

    转自Stackoverflow.备忘用. Question In Python 2 I could do the following: import numpy as np f = lambda x: ...

  2. OSG-CompositeViewer

    原文连接地址:http://www.osgchina.org/index.php?Itemid=490&id=134:usecompositiv&option=com_content& ...

  3. 【转】APP推广什么是cpa,cps,cpm

    转载自:http://www.apptg.cn 经常做做APP推广和做运营的同学对于cpa,cps,cpm,cpc这些名词肯定不会陌生,也基本都知道其表示的含义,但是对于新手来说,这几个词的含义还是不 ...

  4. Siki_Unity_1-9_Unity2D游戏开发_Roguelike拾荒者

    Unity 1-9 Unity2D游戏开发 Roguelike拾荒者 任务1:游戏介绍 Food:相当于血量:每走一步下降1,吃东西可以回复(果子10药水20),被怪物攻击会减少中间的障碍物可以打破, ...

  5. 【setUp-tearDown】线程组开始,结束各执行一次

    使用setUp线程组的方式  ——> 开始 使用tearDown线程组 的方式 ——>结束

  6. 【转】unity3d 资源文件从MAX或者MAYA中导出的注意事项

    转自游戏开发主席   1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择:    1) 导出资源时,只导出一个文件,保留模型,骨骼和所有的动作帧(把所有的动作,比如idle,atta ...

  7. Idea Live Templates

    常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...

  8. 78[LeetCode] Subsets

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  9. [C++] in-class initializer

    C++11 introduced serveral contructor-related enhancements including: Class member initializers Deleg ...

  10. instanceof 运算符简介

    文章摘自: http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ https://developer.mozilla. ...