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 ...
随机推荐
- LSD-FET430UIF与MSP-FET430UIF
成功的破解了没人研究的东西很有成就感!世界需要这样的人!!!LSD-FET430UIF与MSP-FET430UIF?什么是MSPF149?网上查了很多这方面的资料,都没有,最后凭借我阅读PDF的精神和 ...
- jQuery代码不能执行,必须在代码之前就要包含jQuery包
<script> $(function () { $("#btnRegister").click(function () { ...
- python----slots属性安全类
问题:__slots__可以用于构造安全的类.如果一个类使用了__slots__那么它的属性就不在自由了. 下面举例说明: 1.自由属性. class person(object): def __in ...
- SQL Server 空间监测
数据库文件型: select * from sys.dm_db_file_space_usage; go ...
- 我的android studio
- Mindjet 一打开鼠标就动不了解决方法
在网上查找了一下相关资料,这个主要是Mindjet不支持64位系统造成的,其实就和Windows tablet pc input 这个服务相冲突造成的,临时的解决方法是,win+r (别告诉我你不知道 ...
- 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...
- 将类数组对象(array-like object)转化为数组对象(Array object)
用法:Array.prototype.slice.call(array-like object) // 创建一个类数组对象 var alo = {0:"a", 1:"b& ...
- js清空页面控件值
function funClear() {var txts = document.getElementsByTagName("input");for (var i = 0; i & ...
- asp.net预览图片
Aspx code <table> <tr> <td class="style3"> <asp:Label ID="Label1 ...