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 }
  • 相关阅读:
    获取窗口句柄
    人生路上要懂得“刹车”(转)
    关于打开外部程序并且发送一个按键消息 (转)
    CString 和 char * 的相互转换
    iOS开发之了解iPhone中Get和Post方式(转)
    一些简单的Python代码,尝尝Python啥味道 (转)
    iphone 使用委托(delegate)在不同的窗口之间传递数据(转)
    UITextView控件的用法详解 (转)
    iPhone起步1: "Hello,World!" in iPhone Development (转)
    【引用】iphone xml解析流程
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678377.html
Copyright © 2011-2022 走看看