• 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
  • 相关阅读:
    Atom + activate-power-mode震屏插件Windows7下安装
    通过Google身份验证器加强Linux帐户安全
    adb 常用命令总结
    excel 文件加密
    docker 进入容器命令行 /bin/bash 后不支持中文
    无法获取 gcr.io 上的镜像的解决方法
    mysql unix 时间戳转换
    docker 镜像如何导入导出以及建立自己的镜像仓库
    asp.net core 文件的处理
    docker compose 设置环境变量
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6748400.html
Copyright © 2020-2023  润新知