zoukankan      html  css  js  c++  java
  • POJ 2559 Program C

    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 <iostream>  
    2. #include <cstdio>  
    3. using namespace std;  
    4.   
    5. const int N = 100005;  
    6.   
    7. struct Elem  
    8. {  
    9.     int height;  
    10.     int count;  
    11. };  
    12.   
    13. Elem stack[N];  
    14. int top;  
    15.   
    16. int main()  
    17. {  
    18.     int height, n;  
    19.     long long ans, tot, tmp;  
    20.     while (scanf("%d", &n) != EOF && n)  
    21.     {  
    22.         top = 0;  
    23.         ans = 0;  
    24.         for (int i = 0; i < n; ++i)  
    25.         {  
    26.             scanf("%d", &height);  
    27.             tmp = 0;  
    28.             while (top > 0 && stack[top - 1].height >= height)  
    29.             {  
    30.                 tot = stack[top - 1].height * (stack[top - 1].count + tmp);  
    31.                 if (tot > ans) ans = tot;  
    32.                 tmp += stack[top - 1].count;  
    33.                 --top;  
    34.             }  
    35.             stack[top].height = height;  
    36.             stack[top].count = 1 + tmp;  
    37.             ++top;  
    38.         }  
    39.         tmp = 0;  
    40.         while (top > 0)  
    41.         {  
    42.             tot = stack[top - 1].height * (stack[top - 1].count + tmp);  
    43.             if (tot > ans) ans = tot;  
    44.             tmp += stack[top - 1].count;  
    45.             --top;  
    46.         }  
    47.         printf("%lld ", ans);  
    48.     }  
    49.     return 0;  
    50. }  
  • 相关阅读:
    构造 BestCoder Round #52 (div.2) 1001 Victor and Machine
    multiset || 线段树 HDOJ 4302 Holedox Eating
    BFS(最短路) HDOJ 4308 Saving Princess claire_
    组合数专题
    余数专题
    数论 HDOJ 5407 CRB and Candies
    异或+构造 HDOJ 5416 CRB and Tree
    构造 HDOJ 5414 CRB and String
    背包DP HDOJ 5410 CRB and His Birthday
    博客贴自定义高亮代码
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4678546.html
Copyright © 2011-2022 走看看