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 }
  • 相关阅读:
    使用::befor和::after伪元素在网站中添加图标
    使用LESS对CSS进行预处理
    window.innerWidth和document.body.clientWidth的区别
    javascript原型链继承
    javascript中的类型检测
    ASP.NET网站报Service Unavailable错误的解决办法
    在ASP.NET MVC中使用UEditor无法提交的解决办法
    EasyUI DataGrid设置列宽为百分比导致表头和内容错位的解决方法
    使用JS读取本地文本文件(兼容各种浏览器)
    美化上传文件按钮
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678377.html
Copyright © 2011-2022 走看看