zoukankan      html  css  js  c++  java
  • 求矩形的最大面积

    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
    

    Hint

    Huge input, scanf is recommended.
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <stack>
     6 using namespace std;
     7 #define maxn 111111
     8 typedef long long LL;
     9 int a[maxn],L[maxn],R[maxn];
    10 stack<int>S;
    11 int main()
    12 {
    13     int n;
    14     while(scanf("%d",&n)&&n)
    15     {
    16         while(!S.empty()) S.pop();
    17         for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    18         for(int i=1; i<=n; i++)
    19         {
    20             while(!S.empty()&&a[S.top()]>=a[i]) S.pop();
    21             if(!S.empty()) L[i]=S.top();
    22             else L[i]=0;
    23             S.push(i);
    24         }
    25         while(!S.empty()) S.pop();
    26         for(int i=n; i>=1; i--)
    27         {
    28             while(!S.empty()&&a[S.top()]>=a[i]) S.pop();
    29             if(!S.empty()) R[i]=S.top();
    30             else R[i]=n+1;
    31             S.push(i);
    32         }
    33         LL ans=0;
    34         for(int i=1; i<=n; i++) ans=max(ans,(LL)(R[i]-L[i]-1)*a[i]);
    35         printf("%I64d
    ",ans);
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    私有属性的另类访问方式
    获取类所有属性和查看帮助文档
    类的私有属性及私方法(请注意属性的传值方式)
    类的私有属性及私方法
    类的定义
    怎么区分类变量和实例变量?
    面向对象编程案例04--访问控制
    面向对象编程案例03---继承之高级部分
    python 面向对象编程案例01
    静态方法
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678377.html
Copyright © 2011-2022 走看看