[poj 2796]单调栈
题目链接: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]单调栈的更多相关文章
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- POJ 3044单调栈
题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...
- poj 2082 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<iostream> #include<string> #include<cmath> #include ...
- poj 2559 单调栈 ***
给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...
- poj 2599 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
随机推荐
- Python3爬虫(七) 解析库的使用之pyquery
Infi-chu: http://www.cnblogs.com/Infi-chu/ pyquery专门针对CSS和jQuery的操作处理 1.初始化字符串初始化 from pyquery impor ...
- (数据科学学习手札24)逻辑回归分类器原理详解&Python与R实现
一.简介 逻辑回归(Logistic Regression),与它的名字恰恰相反,它是一个分类器而非回归方法,在一些文献里它也被称为logit回归.最大熵分类器(MaxEnt).对数线性分类器等:我们 ...
- ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137
错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...
- git删除本地及远程分支
1. 删除本地分支: git branch -d branchName 2. 删除远程分支: // 方法一:将删除的本地分支推到远程(要删除的远程分支在本地有映射) git push origin : ...
- 1 http协议
1.四层模型 + 2.socket 3.http协议 4. HTTP请求 跟踪了新浪的首页,我们来总结一下HTTP请求的流程: 3.1.1 步骤1:浏览器首先向服务器发送HTTP请求,请求包括: 方法 ...
- Get Error when restoring database in Sql Server 2008 R2
When I restored a database I got an error: "The backup set holds a backup of a database ot ...
- 30分钟 带你浅入seajs源码
上个星期写了浅入requirejs的, 大家都知道 require是AMD规范(Asynchronous Module Definition) 来 今天我们一起看看 CMD规范(Common Mo ...
- 如何理解Java中参数传递只能传值?
以前学习C#的时候,是完全在工作岗位上学习,一些底层较为深入的道理都不是很清楚.如今学习了Java,对于Java参数传递只能传值,不能传引用(指针)感到很困惑,在C#中不是常常说把某个引用传递到函数中 ...
- [转]struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor
package com.sise.action; import java.util.Map; import com.opensymphony.xwork2.Action; import com ...
- Centos6 使用yum快速搭建LAMP环境
1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on ...