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
  • 相关阅读:
    linux下so动态库一些不为人知的秘密(中二)
    linux下so动态库一些不为人知的秘密(中)
    linux下so动态库一些不为人知的秘密(上)
    Linux下gcc编译控制动态库导出函数小结
    解决Linux动态库版本兼容问题
    MySQL按天,按周,按月,按时间段统计【转载】
    MySQL统计函数记录——按月、按季度、按日、时间段统计以及MySQL日期时间函数大全
    RequestMapping中produces属性作用
    出现 java.net.ConnectException: Connection refused 异常的原因及解决方法
    Springboot应用中@EntityScan和@EnableJpaRepositories的用法
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6748400.html
Copyright © 2011-2022 走看看