关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈。

Accepted Code:

 /*************************************************************************
> File Name: 2796.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月21日 星期一 22时45分56秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = ;
int n;
// b[i] holds the smallest index where a[b[i]]...a[i] are not larger than a[i], and e[i] is similar, just extends to the right of a[i]
int a[maxn], b[maxn], e[maxn], st[maxn];
LL sum[maxn]; //prefix sum int
main(void) {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) scanf("%d", a+i);
for (int i = ; i <= n; i++) b[i] = e[i] = i;
sum[] = ;
for (int i = ; i <= n; i++) {
sum[i] = sum[i-] + a[i];
}
// the top of no decreasing stack
int top = ;
for (int i = ; i <= n; i++) {
if (!top || a[i] > a[st[top-]]) {
// push an element to stack
st[top++] = i;
} else {
while (top > && a[st[top-]] > a[i]) {
// update e[st[top-1]]
e[st[top-]] = i - ;
// update b[i]
b[i] = b[st[top-]];
// pop the toppest element of stack
top--;
}
if (!top || a[st[top-]] != a[i]) {
// push
st[top++] = i;
} else if(a[st[top-] == a[i]]) {
// update b[i]
b[i] = b[st[top-]];
}
}
}
// update e[i] of those elements that still in the stack
for (int i = ; i < top; i++) e[st[i]] = n;
// find the answer
LL ans = -, bb, ee;
for (int i = ; i <= n; i++) {
if (ans < a[i] * (sum[e[i]]-sum[b[i]-])) {
ans = a[i] * (sum[e[i]]-sum[b[i]-]);
bb = b[i];
ee = e[i];
}
}
printf("%lld\n%lld %lld\n", ans, bb, ee);
} return ;
}

Poj 2796 单调栈的更多相关文章

  1. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

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

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

  3. Poj 3250 单调栈

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

  4. poj 2059 单调栈

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

  5. POJ 3044单调栈

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

  6. poj 2082 单调栈 ***

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

  7. poj 2559 单调栈 ***

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

  8. poj 2599 单调栈 ***

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

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

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

  10. POJ 2796 Feel Good 【单调栈】

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

随机推荐

  1. 关于Collection接口和Map

    Iterable才是Collection的父接口.不是Iterator. Map,SortedMap属于接口类型,不可以new的方式创建对象. HashMap基于哈希表实现Map接口的类,并允许nul ...

  2. eclipse memory analyzer对系统内存溢出堆文件解析0(转)

    前言 在平时工作过程中,有时会遇到OutOfMemoryError,我们知道遇到Error一般表明程序存在着严重问题,可能是灾难性的.所以找出是什么原因造成OutOfMemoryError非常重要.现 ...

  3. HBase+Redis

  4. [NOI2003]逃学的小孩【观察+树的直径】

    Online Judge:Bzoj1509,Luogu P4408 Label:观察,树的直径 题目描述 输入 第一行是两个整数N(\(3≤N≤200000\))和M,分别表示居住点总数和街道总数.以 ...

  5. Windows API 第20篇 SetVolumeMountPoint 设置卷挂载点参数错误

    函数原型:BOOL SetVolumeMountPoint(                                                   IN   LPCTSTR lpszVo ...

  6. windows下bat批量处理启动exe

    新建文本文档,start.dat start "" "D:\QQ\anzhaung\Bin\QQ.exe" 启动QQ cd ./当前文件夹下,../上一文件夹下 ...

  7. LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...

  8. Django-rest Framework(六)

    不懂使用机制的直接看源码就好了,也不是很难,能够看得懂 视图家族 1. View:将请求方式与视图类的同名方法建立映射,完成请求响应(原生django) from django.views impor ...

  9. 两天了。照着SVN的界面画的一个界面。

      可以选择显示哪些列. 界面上的东西,都简单,麻烦的是它的下层.下层全部用svn server的服务器自带的svn.exe来支持. 有些位置要启动svn.exe不止一次.所以参数的来回传递,来回组合 ...

  10. python-基础-函数-局部和全局变量

    1 函数的定义和调用 1.1 函数定义 1.2 函数的调用 2 函数参数 >>> def fun(a, b, *args, **kwargs): ... ""&q ...