zoukankan      html  css  js  c++  java
  • 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)

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 #define MAXN 100005
     6 #define LL long long
     7 LL L[MAXN];
     8 LL R[MAXN];
     9 LL h[MAXN];
    10 
    11 int main()
    12 {
    13     int n;
    14     while (scanf("%d",&n)&&n)
    15     {
    16         for (int i=1;i<=n;i++)
    17             scanf("%I64d",&h[i]);
    18         h[0]=h[n+1]=-1;
    19         for (int i=1;i<=n;i++) 
    20         {
    21             L[i]=i;
    22             while (h[L[i]-1]>=h[i]) //利用 L[] 去循环,其实很快
    23                 L[i]=L[L[i]-1];
    24         }
    25         for (int i=n;i>=1;i--)
    26         {
    27             R[i]=i;
    28             while (h[R[i]+1]>=h[i])
    29                 R[i]=R[R[i]+1];
    30         }
    31         LL ans = 0;
    32         for (int i=1;i<=n;i++)
    33         {
    34             LL area = (R[i]-L[i]+1)*h[i];
    35             if(area>ans) ans = area;
    36         }
    37         printf("%I64d
    ",ans);
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    [改善Java代码]使用构造块精炼程序
    [改善Java代码]若有必要,使用变长数组
    [改善Java代码]警惕数组的浅拷贝
    [改善Java代码]在明确的场景下,为集合指定初始容量
    [改善Java代码] 枚举项数量限定为64个以内
    [改善Java代码]多种最值算法,适时选择
    [改善Java代码]推荐使用枚举定义常量
    [改善Java代码]性能考虑,数组是首选
    [改善Java代码]用偶判断,不用奇判断
    [改善Java代码]break万万不可忘
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6748400.html
Copyright © 2011-2022 走看看