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 }
  • 相关阅读:
    (转)通过Javascript得到URL中的参数(query string)
    (转)对存储过程进行加密和解密(SQL 2008/SQL 2012)
    (转)怎样玩转千万级别的数据
    (转)mongodb学习(翻译1)
    (转)Web API 强势入门指南
    (转)正则表达式—RegEx(RegularExpressio)(三)
    学习进度-16 python爬虫
    学习进度-15 变量类型的转换
    学习进度-14
    学习进度-13
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678377.html
Copyright © 2011-2022 走看看