两种思路:

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. Selenium系列(五) - 键盘操作详细解读

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  2. hdu3367最大伪森林(并查集)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3367/ 题目要求一个连通图的最大伪森林,伪森林是一个最多有一个回路的图.我们只要用Kruskal最大生成树的策略 ...

  3. hdu1541树状数组(降维打击)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1541/ 题意是:在二维图上有一系列坐标,其中坐标给出的顺序是:按照y升序排序,如果y值相同则按照x升序排序.这个 ...

  4. Cesium 源码笔记[1] Viewer模块实例化的大致过程

    我原本想写日记的,但是不太现实. 源码下载 源码可以从源码包和发行包中的Source目录中获取. Cesium的模块化机制从1.63版本开始,由原来的RequireJs变为ES6.但有可能是原先设计耦 ...

  5. Python电影数据分析

    数据说明:MovieLens数据集,它包含来自于943个用户以及精选的1682部电影的100K个电影打分.每个用户至少为20部电影打分,数据类型user id | item id | rating | ...

  6. 从零开始的JSON库

    从零开始的JSON库 项目地址 jsoncpp ,此项目受到 leptjson 启发,实现了最基本的功能,仅作学习使用. 提供简单的 parse() 和 generate() 方法将 JSON 文本解 ...

  7. 使用FME平移shapefile文件

  8. HashCode()与equals()深入理解

    1.hashCode()和equals()方法都是Object类提供的方法, hashCode()返回该对象的哈希码值,该值通常是一个由该对象的内部地址转换而来的int型整数, Object的equa ...

  9. CAS / ABA

    CAS / ABA 标签(空格分隔): 操作系统 1. CAS 解决 Volatile 不保证原子性的问题 /** * Atomically increments by one the current ...

  10. 基于华为云IoT Studio自助生成10万行代码的奥秘

    华为IoT小助手们搬好板凳.备好笔记本.听了HDC.Cloud的几场华为云技术架构师的直播讲课,感觉获益匪浅却又似懂非懂,直后悔自己没有好好打下基础.为了避免再次出现这样的情况,小助手偷偷跑去找了华为 ...