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 }
  • 相关阅读:
    git使用流程
    php5.3.*编译出现make: *** [ext/gd/libgd/gd_compat.lo] Error 1 解决方法 [转]
    linux用户和组的操作,vi编辑器的使用
    linux目录介绍
    linux和windows分区原理
    linux配置网络,配置lmap
    linux常用操作命令
    mysql数据库编程,内置函数,存储过程(循环插入)
    msql数据库触发器和连接mysql常用函数
    mysql数据库增加删除跟新,关联数据
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678377.html
Copyright © 2011-2022 走看看