题目链接:http://poj.org/problem?id=2796

单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里。

#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std; const int maxn=;
int a[maxn];
int l[maxn];
int r[maxn];
long long pre[maxn];
stack< pair<int,int> > S; int main()
{
int n;
while (~scanf("%d",&n))
{
while (!S.empty()) S.pop();
for (int i=;i<=n;i++) scanf("%d",&a[i]);
a[n+]=-;
S.push(make_pair(-,));
for (int i=;i<=n+;i++)
{
while (S.top().first>=a[i])
{
int p=S.top().second;
S.pop();
l[p]=S.top().second+;
r[p]=i-;
}
S.push(make_pair(a[i],i));
}
pre[]=;
for (int i=;i<=n;i++) pre[i]=pre[i-]+a[i];
long long ans=-;
int ansp=-;
for (int i=;i<=n;i++)
{
long long tt=(pre[r[i]]-pre[l[i]-])*a[i];
if (tt>ans)
{
ans=tt;
ansp=i;
}
}
printf("%lld\n%d %d\n",ans,l[ansp],r[ansp]);
}
return ;
}

[poj 2796]单调栈的更多相关文章

  1. uva 1619 - Feel Good || poj 2796 单调栈

    1619 - Feel Good Time limit: 3.000 seconds   Bill is developing a new mathematical theory for human ...

  2. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  3. poj 2059 单调栈

    题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...

  4. POJ 3044单调栈

    题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...

  5. poj 2082 单调栈 ***

    和poj2082差不多,加了一个宽度的条件 #include<iostream> #include<string> #include<cmath> #include ...

  6. poj 2559 单调栈 ***

    给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...

  7. poj 2599 单调栈 ***

    和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ...

  8. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  9. POJ 2796 Feel Good 【单调栈】

    传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...

随机推荐

  1. 解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误

    解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误 今天安装启动nginx的时候报 ...

  2. POJ1236 tarjan

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19613   Accepted: 77 ...

  3. java泛型<? extends E> 有上限通配符与<? Super E>有上限通配符

    通配符?,?表示占位,表明将来使用的时候在指明类型 <?>无限定的通配符, 是让泛型能够接受未知类型的数据 <? extends E> 有上限通配符,能够接受指定类及其子类类型 ...

  4. C++ vector二维数组

    C++ 构建二维动态数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 这样就构成10*5的数组 ...

  5. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  6. docker制作jdk+tomcat镜像

    docker部署TOMCAT项目 一.内核升级 [root@test01 ~]# uname -r   #内核查看确认 2.6.32-696.16.1.el6.x86_64 [root@test01 ...

  7. 《.NET 微服务:适用于容器化 .NET 应用的体系结构》关键结论

    作为总结和要点,以下是本指南中最重要的结论.1 使用容器的好处: 基于容器的解决方案有节约成本的好处,因为容器是针对生产环境中缺少依赖而导致的部署问题提出的解决方案.容器能够显著改善devops和生产 ...

  8. JavaScript序列化对象成URL格式

    http://access911.net/fixhtm/72FABF1E15DCEAF3.htm?tt=

  9. 预装win8的笔记本如何重装win7

    测试电脑联想T440. 开机按F1,然后Enter,进入Bios设置. 先关闭Secure Boot,然后设置为Legacy Boot. 之后才能设置U盘为第一启动盘. 进入老毛桃的PE系统,使用Di ...

  10. MinGW安装图文教程以及如何配置C语音编程环境

    MinGW安装图文教程以及如何配置C语音编程环境 转载自:http://www.jb51.net/softjc/192017.html MinGW 是一组包含文件和端口库,其功能是允许控制台模式的程序 ...