Largest Rectangle in a Histogram


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11637    Accepted Submission(s): 3197

Problem Description

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.

 

Input

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

 

Output

For 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

 

Source

University of Ulm Local Contest 2003

题目大意:给你一个直方图,告诉你各个条形矩形的高度,求基线对齐构成的矩形中面积

最大的矩形的面积对于每个矩形。面积 = h[i]*(j-k+1),当中j,k是左右边界,h[i]是矩形

的高。而且对于j <= x <= k,h[i] <= h[x]。

本题中,找到左右边界j,k是关键。

利用动态规划的方法,对于位置i,假设左边条形矩形的高度大于它本身,那么左边的左边

界一定也满足位置i的左边界。同理假设右边条形矩形的高度大于它本身,那么右边的右边

界也一定满足位置i的右边界。迭代循环下去。直到找到i的左右边界。

#include<stdio.h>
#include<string.h> int l[100010],r[100010];
__int64 h[100010];
int main()
{
int N;
while(~scanf("%d",&N) && N!=0)
{
memset(h,0,sizeof(h));
for(int i = 1; i <= N; i++)
{
scanf("%I64d",&h[i]);
l[i] = r[i] = i;
} l[0] = 1;
r[N+1] = N;
h[0] = -1;
h[N+1] = -1;
//这上边不加就会超时,不加的话下边就可能一直while,跳不出循环
for(int i = 1; i <= N; i++)
{
while(h[l[i]-1] >= h[i])//找位置i的左边界
l[i] = l[l[i]-1];
}
for(int i = N; i >= 1; i--)
{
while(h[r[i]+1] >= h[i])//找位置i的右边界
r[i] = r[r[i]+1];
}
__int64 MaxArea = -0xffffff0;
for(int i = 1; i <= N; i++)
{
if(h[i]*(r[i]-l[i]+1) > MaxArea)
MaxArea = h[i]*(r[i]-l[i]+1);
}
printf("%I64d\n",MaxArea);
}
return 0;
}

HDU1506_Largest Rectangle in a Histogram的更多相关文章

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

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

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

  3. Largest Rectangle in a Histogram(DP)

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

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

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

  5. Largest Rectangle in a Histogram(HDU1506)

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

  6. POJ 2559 Largest Rectangle in a Histogram

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

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

  8. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

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

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

随机推荐

  1. VC++6.0下通过opencv读入图像并反色

    第一个opencv测试程序: 不多说,直接上代码,代码注释很详尽: ////////////////////////////////////////////////////////////////// ...

  2. 【LeetCode练习题】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  3. OpenStack securityGroup rule Set

    OpenStack DBaas 云数据即服务之☞troveError

  4. PHP的优点

    1.语法简单 2.学习成本低 3.开发效率高 4.跨平台 5.开发部署方便 6.开源框架非常丰富(如:ThinkPHP) 7.开源CMS系统非常丰富(如:Joomla,Wordpress) 8.开源网 ...

  5. UI设计师不可不知的安卓屏幕知识

    不少设计师和工程师都被安卓设备纷繁的屏幕搞得晕头转向,我既做UI设计,也做过一点安卓界面布局,刚好对这块内容比较熟悉,也曾在公司内部做过相关的讲座,在此,我将此部分知识重新梳理出来分享给大家! 1.了 ...

  6. read op case $op in

     read op case $op in

  7. Chrome浏览器切换到之前打开的标签页会重新加载

    这是谷歌的一种策略.当系统内存不足时,系统会自动从内存中舍弃标签页 在地址栏输入chrome://flags/#automatic-tab-discarding,设置为停用即可.

  8. jQuery事件绑定方法bind、 live、delegate和on的区别

    我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 1.准备知识 当我们在开始的时候,有些知识是必须具备的: 1 ...

  9. css系列教程--选择器

    css派生选择器:是指在某个样式表或者dom元素的行内定义行内元素的属性,而其他同名样式在其他dom节点无效的样式表定义方式.例如:div ul li{border:1px solid red;}&l ...

  10. Android 四大组件之Activity生命周期

    写这篇博文之前,已经对android有一定的了解和认识.这篇博文主要讲述android的Activity的生命周期,这是android开发者必须掌握的知识.android的Activity组件拥有7个 ...