Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10873    Accepted Submission(s): 2969

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
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1505 

pid=1058" target="_blank">1058 1176 

pid=2870" target="_blank">2870 2571




题目大意:给你一张柱形图,先给你一个N表示柱的数量。然后给你N个高度。每个柱的宽度是1,让你在柱形图中放入一个矩形。要求输出的矩形面积是最大的。



解题思路:矩形的高度一定是那些给出柱的高度中的一个,以一个柱的高度为矩形的高时计算出当前这个高度下最大的矩形面积,枚举每个高度,最后找到那个最大的,

就是 所求。所以题目就变成了在给定高度下找矩形的最大宽度(高度一定找最大面积),那么就要找到当前高度的左右边界,明显仅仅要旁边的柱比这个高度要高就能够再向旁

边扩展一个单位。解题思路给出。

还好这题没出负数数据,要不然就完蛋了。。。

#include <iostream>
using namespace std;
#define M 100005
__int64 l[M],r[M],vis[M]; //数据用int过不去。所以用__int64.
__int64
max(__int64 x,__int64 y){
return
x>y?x:y;
}
int
min(int x,int y){
return
x<y?x:y;
}
__int64
DP(__int64 a[],__int64 n){
__int64
i;
for(
i=1;i<=n;i++) //从左边找左边界,从右边開始找会超时,不解释。 {
while(
a[i]<=a[l[i]-1]) l[i]=l[l[i]-1]; //假设左边的比当前高度高,说明能够向左边扩展。while这里有点递归的味道。
//直到左边比当前高度低,才结束while。 }
for(
i=n;i>=1;i--)
{
while(
a[i]<=a[r[i]+1]) r[i]=r[r[i]+1]; //找右边界的过程和找左边界过程一样。 }
__int64
tot=0;
for(
i=1;i<=n;i++) //每个高度的最大宽度已经找到,计算最大面积。 {
tot=max(tot,(r[i]-l[i]+1)*a[i]); //宽度为1。所以还要+1.
}
return
tot;
} int main(__int64
i,__int64 j,__int64 k)
{
__int64
n,cur;
while(
scanf("%I64d",&n)!=EOF&&n)
{
l[0]=l[n+1]=vis[0]=vis[n+1]=-1; //这里是防一些特殊的数据。按理来说取0就能够了。只是过不了。
 r[n+1]=n;
for(
i=1;i<=n;i++)
{

l[i]=r[i]=i; //l[i]记录的是vis[i]的左边界,r[i]记录vis[i]的右边界。
scanf("%I64d",&vis[i]);
}

cur=DP(vis,n);
printf("%I64d\n",cur);
}
return
0;
}

HDU1506 Largest Rectangle in a Histogram (动规)的更多相关文章

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

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

  2. hdu1506——Largest Rectangle in a Histogram

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

  3. hdu1506 Largest Rectangle in a Histogram

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  4. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  5. 【题解】hdu1506 Largest Rectangle in a Histogram

    目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...

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

  7. HDU1506 ( Largest Rectangle in a Histogram ) [dp]

    近期情绪太不稳定了.可能是由于在找实习这个过程碰壁了吧.第一次面试就跪了,可能是我面的是一个新公司,制度不完好,我感觉整个面试过程全然不沾编程,我面试的还是软件开发-后来我同学面试的时候.说是有一道数 ...

  8. 【单调栈】hdu1506 Largest Rectangle in a Histogram

    单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是 ...

  9. HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)

    单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来 ...

随机推荐

  1. selenium - 弹出框操作

    # 6. 弹出框操作 # 6.1 页面弹出框操作# 页面弹出框 是一个html页面的元素,由用户在页面的操作触发弹出# (1)执行触发操作之后,等待弹出框出现之后,# (2)再定位弹出框中的元素并操作 ...

  2. 【POJ 2585】Window Pains 拓扑排序

    Description . . . and so on . . . Unfortunately, Boudreaux's computer is very unreliable and crashes ...

  3. 80x86保护模式下IDT和中断调用过程分析

    80x86保护模式下IDT和中断调用过程分析 1.中断描述符表(IDT),将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT类似,IDT也是由8字节长度的描述符组成.IDT空描述符的存 ...

  4. Facebook App 的头文件会有更多的收获

    最近在看一些 App 架构相关的文章,也看了 Facebook 分享的两个不同时期的架构(2013 和 2014),于是就想一窥 Facebook App 的头文件,看看会不会有更多的收获,确实有,还 ...

  5. 【转】C# 中的"yield"使用

    C# 中的"yield"使用 yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写 ...

  6. WebSocket & websockets

    WebSocket & websockets https://en.wikipedia.org/wiki/WebSocket https://developer.mozilla.org/en- ...

  7. ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)

    Problem D: (ds:树)合并果子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solved: 4 [Submit][Status][ ...

  8. HDU-1534 Schedule Problem

    四种约束条件..照做就行了.. 最长路建图. #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  9. [HNOI2006]公路修建问题 (二分答案,并查集)

    题目链接 Solution 二分答案+并查集. 由于考虑到是要求花费的最小值,直接考虑到二分. 然后对于每一个二分出来的答案,模拟 \(Kruskal\) 的过程再做一遍连边. 同时用并查集维护联通块 ...

  10. php错误报告

    ; This directive controls whether or not and where PHP will output errors, ; notices and warnings to ...