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 rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
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.
InputThe input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.OutputFor each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
// 第一个数 n ,代表后面有几个数,后面 n 个数代表长方体的高,宽都是 1 ,并且n个长方体紧靠,问最大的矩形面积
L[i] 代表 h[j]>=h[i](j<=i) 的最远的编号
R[i] 代表 h[j]>=h[i](j>=i) 的最远的编号
求出来后,就可以去找最大矩形了,遍历一遍 maxS = max( (R[i]-L[i]+1)*h[i] ) (1<=i<=n)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 100005
#define LL long long
LL L[MAXN];
LL R[MAXN];
LL h[MAXN]; int main()
{
int n;
while (scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++)
scanf("%I64d",&h[i]);
h[]=h[n+]=-;
for (int i=;i<=n;i++)
{
L[i]=i;
while (h[L[i]-]>=h[i]) //利用 L[] 去循环,其实很快
L[i]=L[L[i]-];
}
for (int i=n;i>=;i--)
{
R[i]=i;
while (h[R[i]+]>=h[i])
R[i]=R[R[i]+];
}
LL ans = ;
for (int i=;i<=n;i++)
{
LL area = (R[i]-L[i]+)*h[i];
if(area>ans) ans = area;
}
printf("%I64d\n",ans);
}
return ;
}
Largest Rectangle in a Histogram (最大子矩阵)的更多相关文章
- HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- 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 ...
- Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- Largest Rectangle in a Histogram(HDU1506)
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
随机推荐
- wireshark问题现象分析
讲的非常透彻:建议学习 wireshark问题现象分析1:参考博客1 https://blog.csdn.net/u012398362/article/details/52276067 wiresha ...
- java.lang.NoSuchFieldError:INSTANCE
Java.lang.NoSuchFieldError: INSTANCE异常,可能是包重复了. 我遇到的情况是maven里引入了一个JAR,而我又在lib里面引入了这个jar,并且版本还不相同,就出了 ...
- IQueryable接口与IEnumberable 区别
总结一下: IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,t ...
- 编译安装Nginx和php搭建KodExplorer网盘
编译安装Nginx和php搭建KodExplorer网盘 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 nginx-1.12.2 php ...
- AngularJS: Dynamically loading directives
http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/ ----------------------- ...
- Material Design (二),TextInputLayout的使用
前言 一般登录注冊界面都须要EditText这个控件来让用户输入信息,同一时候我们通常会设置一个标签(使用TextView)和EditText的hint属性来提示用户输入的内容,而设计库中高级组件T ...
- Nginx:处理HTTP请求
参考资料<深入理解Nginx>(陶辉) 处理HTTP请求 接着上一次的内容,本次将说明HTTP框架是如何召集负责具体功能的各HTTP模块合作处理请求的. 在http://www.cnblo ...
- Android Shader 颜色、图像渲染 paint.setXfermode
Shader Shader是一个基类,表示在绘制期间颜色的水平跨度 它的子类被嵌入在Paint中使用,调用paint.setShader(shader). 除Bitmap外的其他对象,使用该Paint ...
- Linux下Java、Maven、Tomcat的安装
1.安装Java(此处假定安装文件夹位/usr/local) 1)下载jdk(jdk-7),下载地址例如以下: 32位:http://download.oracle.com/otn-pub/java/ ...
- Android中多线程编程(三)Handler更新UI的方式
Handler更新UI的方式和原因以及遇到的问题 1.方式: 仅仅能通过Handler来更新UI. 代码例如以下: package com.chengdong.su.handlerdemo; impo ...