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 }
  • 相关阅读:
    RocketMQ——角色与术语详解
    大数据学习方向
    连接mysql数据库报错java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.
    exception is feign.RetryableException: Connection refused (Connection refused) executing GET http://......
    【BZOJ3261】最大异或和(可持久化Trie)
    【BZOJ4552】排序(线段树,二分)
    【CodeChef】LECOINS(同余最短路,背包DP)
    【BZOJ2118】墨墨的等式(同余最短路)
    【BZOJ5249】IIIDX(贪心,线段树)
    【gym102394L】LRU Algorithm(自然溢出哈希)
  • 原文地址:https://www.cnblogs.com/Penn000/p/5800163.html
Copyright © 2011-2022 走看看