zoukankan      html  css  js  c++  java
  • Largest Rectangle in a Histogram(HDU 1506 动态规划)

    Largest Rectangle in a Histogram

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


    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
    求出每个矩形最左可延伸点和最右可延伸点,这里用到了迭代寻找最远点,普通的寻找会超时。然后计算每个矩形的所能扩展的面积。
     1 #include <stdio.h>
     2 #define Max 100005
     3 __int64 dpl[Max],dpr[Max],h[Max];
     4 int main()
     5 {
     6     __int64 n,k,m;
     7     int i,j;
     8     freopen("in.txt","r",stdin);
     9     while(scanf("%I64d",&n)!=EOF)
    10     {
    11         if(n==0)
    12             break;
    13         m=0;
    14         for(i=1;i<=n;i++)
    15         {
    16             scanf("%I64d",&h[i]);
    17             dpr[i]=dpl[i]=i;
    18         }
    19         h[n+1]=h[0]=0;
    20         for(i=n;i>=1;i--)
    21         {
    22             while(h[dpr[i]+1]>=h[i])
    23                 dpr[i]=dpr[dpr[i]+1];
    24         }
    25         for(i=1;i<=n;i++)
    26         {
    27             while(h[dpl[i]-1]>=h[i])
    28                 dpl[i]=dpl[dpl[i]-1];
    29         }
    30         for(i=1;i<=n;i++)
    31         {
    32             k=(dpr[i]-dpl[i]+1)*h[i];
    33             if(m<k)    m=k;
    34         }
    35         printf("%I64d
    ",m);
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    SparkSQL UDF使用方法与原理详解
    Hadoop 之日志管理—应用在 YARN 中运行时的日志
    圣诞节雪花效果,pc端
    转 redis使用场景 简介
    转 springboot 教程
    springboot freeMarker
    springboot web 服务器选择
    springboot 异步任务
    springboot 整合 rabbitmq
    软件开发生命周期过程中,必须考虑的安全问题
  • 原文地址:https://www.cnblogs.com/a1225234/p/5277274.html
Copyright © 2011-2022 走看看