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 (最大子矩阵)的更多相关文章

  1. HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...

  2. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  4. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  5. 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 ...

  6. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  7. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  8. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  9. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

随机推荐

  1. Unable to list target platforms. Please make sure the android sdk path is correct. See the Console for more details.

    在android上发布遇到 androidSDK无法找到的问题 http://www.jianshu.com/p/fe4c334ee9fe

  2. Shell--变量的显示与设置、环境变量、语系变量

    1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来  格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...

  3. 配置Linux实现静态路由

    配置Linux实现静态路由 背景和原理 路由器的功能是实现一个网段到另一个网段之间的通信,路由分为静态路由.动态路由. 默认路由和直连路由.静态路由是手工指定的,使用静态路由的好处是网络安全保密性高. ...

  4. PHP使用mysqli连接MySQL数据库

    使用mysqli函数库连接MySQL,支持面向对象和面向过程两种方式: 1.面向对象的使用方式 建立一个连接 $db = new mysqli('localhost', 'root', '123456 ...

  5. vue笔记一

    一.Vue基本知识点 1.声明式渲染 <div id="app">{{ msg }}</div> <script> var app = new ...

  6. 【Java】Java_19递归算法

    1.递归算法 A方法调用B方法,我们很容易理解!递归就是:A方法调用A方法!就是自己调用自己,因此我们在设计递归算法时,一定要指明什么时候自己不调用自己.否则,就是个死循环! 1.1递归算法要点 递归 ...

  7. Ubuntu下安装配置和卸载Tomcat

    转载自:http://zyjustin9.iteye.com/blog/2177291 注:此处不采用apt-get安装,因为这种方式安装后,tomcat安装目录会到处都是,像天女散花一样.此处采取下 ...

  8. [腾讯 TMQ] 零基础学习 Fiddler 抓包改包

    本文转载于https://testerhome.com/topics/7159 一.Fiddler1.1.简介Fiddler是一款HTTP协议调试代理工具,它能够抓取记录本机所有HTTP(S)请求,通 ...

  9. ubuntu下设置静态ip

    直接修改系统配置文件ubuntu的网络配置文件是:/etc/network/interfacesubuntu命令行修改网络配置方法前面auto  eth?,让网卡开机自动挂载. 为网卡配置静态IP地址 ...

  10. 深入探析 Rational AppScan Standard Edition 新特性之 Glass Box 扫描

    众所周知,Web 应用安全测试通常有黑盒安全测试和白盒安全测试两种方法.这两种方法孰优孰劣一直众议纷纷.广为公认的是,这两种测试方法有着良好地互补性,两种测试方法的结合是未来安全测试技术的发展趋势.G ...