CCF_ 201312-3_最大的矩形

遍历数组中每一元素,左右延伸得出宽度。

 

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    int n,a[1005],sum = 0;
    cin >> n;
    for(int i = 1;i <= n;i++)    cin >> a[i];
    for(int i = 1;i <= n;i++)
    {
        int j,left = -1,right = -1;
        for(j = i-1;j > 0;j--)
        {
            if(a[j] < a[i])
            {
                left = j;
                break;
            }
        }
        if(left == -1)    left = 0;
        for(j = i+1;j <= n;j++)
        {
            if(a[j] < a[i])
            {
                right = j;
                break;
            }
        }
        if(right == -1)    right =    n+1;
        sum = max(sum,(right-left-1)*a[i]);
    }
    cout << sum << endl;
    return 0;
}

 

posted @ 2016-09-06 02:44  zzzzzzzzhu  阅读(360)  评论(0编辑  收藏  举报