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

  把数一个一个丢进单调栈,弹出的时候[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」conda与pip升级所有的包

    conda: conda update --a pip: pip freeze --local | grep -v '^-e' | cut -d = -f 1 | xargs -n1 sudo pip ...

  2. 「赛后补题」Meeting(HDU-5521)

    题意 A,B两个人分别在1和n区.每个区有若干点(区之间的点可以重复,各个区内点间的距离一致),给出区之间有联系的图以及到达所需时间.求两个人见面最短时间以及在哪个区碰面(可有多个) 分析 隐式图搜索 ...

  3. OSG-阴影

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  4. Python简要标准库(1)

    sys sys这个模块让你能够访问与Python解释器联系紧密的变量和函数 其中的一些在下表 F argv 命令行参数,包括脚本名称 exit([arg]) 退出当前的程序,可选参数为给定的返回值或者 ...

  5. [CF106C]Buns

    面包师Lavrenty打算用馅料做几个面包,然后把它们卖掉. Lavrenty有\(n\)克面团和\(m\)种不同的馅料.馅料种类的下标从\(1到m\),他知道他的第\(i\)种馅料剩下\(a_i\) ...

  6. python 终极篇 ---django 认证

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...

  7. (一)Spring Boot修改内置Tomcat端口号--解决tomcat端口被占用的问题

    Spring Boot 内置Tomcat默认端口号为8080,在开发多个应用调试时很不方便,本文介绍了修改 Spring Boot内置Tomcat端口号的方法. 一.EmbeddedServletCo ...

  8. EasyUI学习心得

    因为要修改十几年前的一个项目界面,打9月份开始学习EasyUI,很多事情都要自己试过才知道,小问题会浪费很多时间.所以,就在此记录一下,随时更新. 一.引号 EasyUI的自定义关键字的识别,API文 ...

  9. python邮件服务-yagmail

      下载安装 yagmail import yagmail #链接邮箱服务器 #此处的password是授权码 yag= yagmail.SMTP( user="843092012@qq.c ...

  10. JavaScript 常用控制流程代码范例

    if-else 的用法 var a = 33 if (a == 1){ console.log ('a等于1') } else if (a==2) { console.log ('a等于2') } e ...