zoukankan      html  css  js  c++  java
  • hdu 1506 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.
    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.
    题意描述:给出一些宽度为1,高度大于等于0的矩形,求出最大的矩形面积。
    算法分析:看到n的范围,就确定不能n*n了,想了想,对于一个矩形i ,找到最左方和最右连续都比它高的位置之后,对于矩形i+1来说,如果i+1高度比i小,那么对i+1来往左扩展找出左方连续比它高的最左方的位置时候,就没有必要再次比较i+1和i、i+1和i-1、、、因为前面有一部分由 i 已经比较过了,所以我们只需要从上次的记录的位置开始往左比较即可。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define inf 0x7fffffff
     8 using namespace std;
     9 typedef long long LL;
    10 const int maxn=100000+10;
    11 
    12 int n;
    13 int l[maxn],r[maxn],an[maxn];
    14 
    15 int main()
    16 {
    17     while (scanf("%d",&n)!=EOF && n)
    18     {
    19         for (int i=1 ;i<=n ;i++) {scanf("%d",&an[i]);l[i]=r[i]=i;}
    20         an[0]=an[n+1]=-1;
    21         l[0]=l[n+1]=r[0]=r[n+1]=0;
    22         for (int i=1 ;i<=n ;i++)
    23         {
    24             while (an[l[i]-1 ]>=an[i])
    25                 l[i]=l[l[i]-1 ];
    26         }
    27         for (int i=n ;i>=1 ;i--)
    28         {
    29             while (an[r[i]+1 ]>=an[i])
    30                 r[i]=r[r[i]+1 ];
    31         }
    32         LL ans=0;
    33         for (int i=1 ;i<=n ;i++)
    34         {
    35             LL area=(LL)(r[i]-l[i]+1)*(LL)an[i];
    36             if (area>ans) ans=area;
    37         }
    38         printf("%I64d
    ",ans);
    39     }
    40     return 0;
    41 }
     
  • 相关阅读:
    路由器 命令行基础
    docker 学习
    flume 配置
    CentOS 7 安装字体库 & 中文字体
    centos7 web服务器内核优化
    hive 搭建
    varnish4.1 配置文件default.vcl
    varsh4.1 安装清除cache
    jvm 配置
    centos7优化内核参数详解
  • 原文地址:https://www.cnblogs.com/huangxf/p/4458791.html
Copyright © 2011-2022 走看看