两种思路:

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. 升级 nop 4.1 Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.

    Incorrect syntax near 'OFFSET'.  Invalid usage of the option NEXT in the FETCH statement. nop.web 项目 ...

  2. [二分]codeforces 274A k-Multiple Free Set

    k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. go:数据类型

    Go语言中有丰富的数据类型,除了基本的整型.浮点型.布尔型.字符串外,还有数组.切片.结构体.函数.map.通道(channel)等.Go 语言的基本类型和其他语言大同小异. 1.基本数据类型 整型 ...

  4. 强化学习之四:基于策略的Agents (Policy-based Agents)

    本文是对Arthur Juliani在Medium平台发布的强化学习系列教程的个人中文翻译,该翻译是基于个人分享知识的目的进行的,欢迎交流!(This article is my personal t ...

  5. Jmeter接口测试之参数传递(十三)

    在接口自动化测试中,经常会遇到的一种场景就是参数的场景,比如在用户列表中获取所有的用户列表,然后获取到某一个用户的ID,查看该用户的详细信息.首先在这里理清思路,它的流程是,首先获取到数据,然后在Jm ...

  6. SpringBoot 监控中心

    1,SpringBoot 监控中心: 针对微服务服务监控,服务器内存内存变化(对内存,线程,日志管理),检测服务配置连接地址是否可用(模拟访问,懒加载),故意将mysql 数据源连接密码写错,启动就会 ...

  7. js拖拽效果的实现及原理

    元素拖拽分成3个步骤:按下鼠标,移动鼠标,松开鼠标. 拖拽原理:按下拖拽元素后开始监听文档中鼠标移动事件,然后再监听鼠标松开事件:鼠标移动时,元素div要随着鼠标一起移动,需要计算元素div位移的距离 ...

  8. 并查集例题02.带权并查集(poj1182)

    Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...

  9. 实验十一 MySQLl备份与恢复1

    实验十一 MySQL备份与恢复 一.  实验内容: 1. 使用SQL语句导入和导出表数据 2. 使用客户端工具备份还原数据库 3. 使用日志文件恢复数据库 二.  实验项目:学生成绩数据库 创建用于学 ...

  10. B【SDOI2008】Sandy的卡片

    时间限制 : 5000 MS   空间限制 : 128000 KB 问题描述 Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡片 ...