(尚學杯)B.Blind Father

Description

Mr. Leng, who is the predominately inheritor of buried-love family  (One of the most vogue families during your primary school maybe, anyway, this is unimportant), has such cool, cooler and coolest temperament. But, only a good ACMer can be the real leader, in other words, be admitted as a successor.

One day, a problem come to Mr. Leng 's father about carpentry. There are N pieces of planks upright the ground in one line. Each one width 1 and height uncertain.  His father wants to cut some of them and then get a rectangle. How the biggest rectangle does him can make? It too difficult to his father to solve the problem, but it is really easy for Mr. Leng. So do you. Please surmount the problem as soon as you can.

Ps: You can't move or change the posture or position of any planks.

Input

There are multiple cases. In each cases, the first line only contains an integer N, 

means the number of the planks. And the second line contains N numbers, means the height of  N planks one by one.

1<=N<=10000



Output
Please output the biggest rectangle that Mr. Leng 's father can get.
Sample Input
3
10 6 10
Sample Output
18
Hint

樣例圖形

這題數據比較水,O(N^2)就能過

O(n)枚舉中心點,然後向左邊和右邊找到一個位子:

L.使得a【L~mid】>=a【mid】&&a【L-1】<a【mid】;

R.使得a【mid~R】>=a【mid】&&a【R+1】<a【mid】;

那麼對應當前中心點可以發散出一個a【mid】爲高度,R-L+1爲寬度的矩形。


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
long long int a[100050];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        long long int output=0;
        for(int i=0;i<n;i++)
        {
            int L=i;
            int R=i;
            //找出L的最小值
            for(int j=i;j>=0;j--)
            {
                if(a[j]>=a[i])L=j;
                else break;
            }
            //找出R的最大值
            for(int j=i;j<n;j++)
            {
                if(a[j]>=a[i])R=j;
                else break;
            }
            output=max(output,a[i]*(R-L+1));
        }
        printf("%lld\n",output);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章