两种思路:

1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离加上栈顶元素到第二个栈顶元素的距离。

code:

  

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1E5+;
ll arr[N];
int main(){
ll n;
while(cin>>n,n){
ll ans=;
stack<ll >st;
for(ll i=;i<=n;i++) cin>>arr[i];
arr[n+]=;
for(ll i=;i<=n+;i++){
if(st.empty()||arr[st.top()]<=arr[i]) st.push(i);
else {
ll c=st.top();
while(st.size()&&arr[c]>arr[i]){
st.pop();
if(st.empty()) ans=max(ans,arr[c]*(i-));
else {
ans=max(ans,(i-st.top()-)*arr[c]);
c=st.top();
}
}
st.push(i);
}
}
cout<<ans<<endl;
}
return ;
}

2 dp

  维护两个数组left和right,left[i]表示元素i向大于当前元素向左的最大连续延伸。right[i]同理。

     转移方式:j=left[j]-1,j=right[j]+1。然后遍历每个元素ans=max(ans,(right[i]-left[i]+1)*arr[i])

  code:

  

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1E5+;
ll arr[N];
ll left1[N];
ll right1[N];
int main(){
ll n;
while(scanf("%lld",&n)!=EOF&&n){
for(ll i=;i<=n;i++){
scanf("%lld",&arr[i]);
left1[i]=right1[i]=i;
}
for(ll i=;i<=n;i++){
ll j=i;
while(j>=&&arr[i]<=arr[j]) j=left1[j]-;
left1[i]=j+;
}
for (ll i=n-; i>=; i--) {
ll j=i;
while (j<=n && arr[i]<=arr[j]) j=right1[j]+;
right1[i]=j-;
}
ll ans=;
for(ll i=;i<=n;i++){
ans=max(ans,(right1[i]-left1[i]+)*arr[i]);
}printf("%lld\n",ans);
}
return ;
}

 

V - Largest Rectangle in a Histogram HDU - 1506的更多相关文章

  1. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  2. Day8 - C - Largest Rectangle in a Histogram HDU - 1506

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  5. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法

    今天使用pip安装第三库时,有时会报错: pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='file ...

  2. CF230B T-primes 题解

    原题链接 简要题意: 判断一个数是否只有 \(3\) 个因数. 首先,如果一个数有奇数个因数,那么这个数是完全平方数. 道理很简单:因数是成对的,那么必然存在 \(k^2 = n\),此时 \(k\) ...

  3. 题解 P1457 【城堡 The Castle】

    来讨论区大摇大摆地逛了一圈后,我发现竟然大家的代码 都很长 然而代码真的要写那么长吗 首先,来分析问题,1,2,4,8,这些数显然是有特点的,也许你已经想到了没错,它们都是2的次方数. 1是2的0次方 ...

  4. 常见排序算法总结与分析之交换排序与插入排序-C#实现

    前言 每每遇到关于排序算法的问题总是不能很好的解决,对一些概念,思想以及具体实现的认识也是模棱两可.归根结底,还是掌握不够熟练.以前只是看别人写,看了就忘.现在打算自己写,写些自己的东西,做个总结.本 ...

  5. [树的深度] Party

    Party A company has n employees numbered from 1 to n. Each employee either has no immediate manager ...

  6. ysoserial-C3P0 分析

    环境准备: pom: <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> < ...

  7. 面试刷题30:SpringBean的生命周期?

    spring是Java软件开发的事实标准. 我是李福春,我在准备面试,今天的问题是:springBean的生命周期是怎样的? 答:spring最基础的能力是IOC(依赖注入),AOP(面向切面编程), ...

  8. [noip模拟]难缠的值周生<宽搜>

    难缠的值周生 [问题描述] 小 P 上学总是迟到,迟到了以后常常会被值周生发现.被值周生发现就会给他所在的班级扣分,被扣了分不免要挨班主任的训,这令小 P 很不爽.不过,聪明的他经过观察发现,值周生通 ...

  9. Ubuntu+Hexo+Github搭建个人博客

    Ubuntu+Hexo+Github搭建个人博客 目录 目录 目录 1. 简介 环境 2. Git安装及配置 2.1 安装Git 2.2 创建Git仓库 2.3 配置git仓库 2.4 添加公钥 3. ...

  10. XDebug的配置和使用

    简介 XDebug是一个开放源代码的PHP程序调试器(即一个Debug工具) 可以用来跟踪,调试和分析PHP程序的运行状况 功能强大的神器,对审计有非常大的帮助. 官网:http://www.xdeb ...