Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15013 Accepted Submission(s): 4357
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.4 1000 1000 1000 1000
0
4000
题解:让求最大矩形面积,宽为1,暴力超时
可以发现 当第i-1个比第i个高的时候 比第i-1个高的所有也一定比第i个高
于是可以用到动态规划的思想
令left[i]表示包括i在内比i高的连续序列中最左边一个的编号 right[i] 为最右边一个的编号
那么有 当 h[left[i]-1]>=h[i]]时 left[i]=left[left[i]-1] 从前往后可以递推出left[i]
同理 当 h[right[i]+1]>=h[i]]时 right[i]=right[right[i]+1] 从后往前可递推出righ[i]
最后答案就等于 max((right[i]-left[i]+1)*h[i]) 了;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
const int MAXN=;
typedef long long LL;
LL a[MAXN];
int l[MAXN],r[MAXN];
int main(){
int N;
while(scanf("%d",&N),N){
for(int i=;i<=N;i++)scanf("%lld",&a[i]),l[i]=i,r[i]=i;
a[]=a[N+]=-;
for(int i=;i<=N;i++){
while(a[l[i]-]>=a[i])
l[i]=l[l[i]-];
}
for(int i=N;i>=;i--){
while(a[r[i]+]>=a[i])
r[i]=r[r[i]+];
}
LL ans=;
for(int i=;i<=N;i++){
ans=max(ans,(r[i]-l[i]+)*a[i]);
}
printf("%lld\n",ans);
}
return ;
}
Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)的更多相关文章
- Largest Rectangle in a Histogram(HDU 1506 动态规划)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- 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 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- Largest Rectangle in a Histogram 常用技巧 stack的运用
Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- 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 ...
随机推荐
- python基础笔记-0
python中数据结构,主要有列表.元组.字典.集合. python中最基本数据结构是序列(sequence).序列中每个元素被分配一个序号——即元素位置,也成为索引.第一个索引是0,第二个是1,以此 ...
- 工作中的第一份LoadRunner脚本
录制的第一份脚本 虽然是第一份但是调试执行,跑场景等都成功了. Action() { web_url("login1.jsp", "URL=http://192.168. ...
- TableViewController的添加,删除,移动
#import "RootTableViewController.h" @interface RootTableViewController () { UITableViewCel ...
- getHibernateTemplate().find()
find(String queryString, Object[] values); 这个方法后者的参数必须是一个数组,而不能是一个List. List ul=getHibernateTemplate ...
- 【Lucene】挖掘相关搜索词
搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索.一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表.另一种方法 ...
- JavaEE Tutorials (23) - 资源适配器和契约
23.1什么是资源适配器362 23.1.1管理契约363 23.1.2通用工作上下文契约364 23.1.3出站和入站契约36423.2元数据注解36523.3公共客户端接口36623.4对Java ...
- C语言的本质(8)——副作用与顺序点
C 语言中,术语副作用是指对数据对象或者文件的修改.例如以下语句 var = 99; 的副作用是把 var 的值修改成 99.对表达式求值也可能产生副作用,例如: se = 100 对这个表达式求值所 ...
- html和js
1.<input type="button" value="Hello world!"> 2.<button type="butto ...
- 【Python】iichats —— 命令行下的局域网聊天程序
转载请声明出处:http://www.cnblogs.com/kevince/p/3941728.html ——By Kevince ii系列工具第三弹,命令行下的局域网聊天程序 原理: 程序启动 ...
- wdos相关问题解答
wdos系统自动分区的大小说明 wdOS系统提供了可自动分区和手工分区 自动分区适用大部分新手或对分区没有特的要求的人 手工分区适用老手或熟悉分区或有特别需求的人 具体用哪个,没多大区别,关键是看应用 ...