zoukankan      html  css  js  c++  java
  • HDU1506

    Largest Rectangle in a Histogram

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


    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 //2016.8.23
     2 #include<iostream>
     3 #include<cstdio>
     4 #define ll __int64
     5 
     6 using namespace std;
     7 
     8 const int N = 100005;
     9 ll h[N];
    10 int l[N], r[N];//l[i]表示i点向左能够扩展的最远点下标,r[i]表示i点向右最远点下标
    11 
    12 int main()
    13 {
    14     int n;
    15     while(cin>>n&&n)
    16     {
    17         for(int i = 1; i <= n; i++)
    18             scanf("%I64d", &h[i]);
    19         l[1] = 1, r[n] = n;//初始化边界
    20         for(int i = 2; i <= n; i++)//只要高度比h[i]高,一直向左扩展
    21         {
    22             int tmp = i;
    23             while(tmp>1 && h[tmp-1] >= h[i])tmp = l[tmp-1];
    24             l[i] = tmp;
    25         }
    26         for(int i = n-1; i >= 1; i--)//只要高度比h[i]高,一直向右扩展
    27         {
    28             int tmp = i;
    29             while(tmp<n && h[tmp+1] >= h[i])tmp = r[tmp+1];
    30             r[i] = tmp;
    31         }
    32         ll ans = 0;
    33         for(int i = 1; i <= n; i++)
    34         {
    35             ll tmp = h[i]*(r[i]-l[i]+1);//矩形面积,高×底
    36             if(ans < tmp)ans = tmp;
    37         }
    38         printf("%I64d
    ", ans);    
    39     }
    40 
    41     return 0;
    42 }
  • 相关阅读:
    VS2010开发工具使用技巧<之简单讲解>
    JS中的数学计算<之简单实例讲解>
    让DIV中文字换行显示
    VS2010编写WebService与在IIS的发布<之简单讲解>
    JSON.parse 与 eval() 对于解析json的问题
    [字符串]与[数组]的互相转换
    A标签实现文件下载功能
    "Chinese_PRC_CI_AS" 和 "Chinese_PRC_90_CI_AI" 之间的排序规则冲突问题
    IE开发人员工具之实用功能讲解
    CSS选择器 < ~ +
  • 原文地址:https://www.cnblogs.com/Penn000/p/5800163.html
Copyright © 2011-2022 走看看