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:staticmethod vs classmethod
Being educated under Java background, static method and class method are the same thing. But not so ...
- linux查看系统的日志的一些实用操作
last -a 把从何处登入系统的主机名称或ip地址,显示在最后一行. -d 指定记录文件.指定记录文件.将IP地址转换成主机名称. -f <记录文件> 指定记录文件. -n <显示 ...
- 了解单位em和px的区别
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...
- MFC中SQLite数据库的使用
1打开数据库 BOOL playDlg::openData() { WCHAR a[100]; CString path; path = m_exePath+L"sentence_makin ...
- 理解Java多态
多态又称Polymophism,poly意思为多,polymophism即多种形态的意思.一种类型引用因为指向不同的子类,表现出不同的形态,使用不同的方法. 什么是多态 多态建议我们编码时使用comm ...
- e = e || window.event用法细节讨论
e = e || window.event是我们在做事件处理时候区分IE和其他浏览器事件对象时常用的写法.但是这行兼容性代码有没有必要出现在所有的事件句柄中呢?标准事件调用方式需要这行代码吗?下边我们 ...
- C#格式化成小数
datagridview某列格式化成两位小数 ............................................................................. ...
- node.js(三)url处理
1.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看如下示例: var url = require('url'); url.parse('http://ww ...
- 图片的像素和Android的dp值之间的关系。
这是一个困扰我很就得问题.今天在我的反复摸索下,总结出了一些个规律. 以下测试以魅族mx5为例. 手机参数:5.5英寸:高:1920:宽1080. /** * 获得屏幕的宽度 * * @param c ...
- BZOJ 3672: [Noi2014]购票( 树链剖分 + 线段树 + 凸包 )
s弄成前缀和(到根), dp(i) = min(dp(j) + (s(i)-s(j))*p(i)+q(i)). 链的情况大家都会做...就是用栈维护个下凸包, 插入时暴力弹栈, 查询时就在凸包上二分/ ...