gdufe acm 1181 百度的面试

来源:互联网 发布:游族网络虚假宣传 编辑:程序博客网 时间:2024/06/11 16:51

题目链接:gdu 1181

Problem Description:

在一个二维平面,从左到右竖立n根高度分别为:a[1],a[2],….a[n],且宽度都为1的木板。试给出平面内最大的矩阵面积。
这里写图片描述

Input:

输入包含多组测试数据,每一组数据的第一行输入正整数n(<=5000),下面一行输入序列a[1],a[2],…a[n],其中1<=a[i]<=1000。

Output:

对于每一组测试数据,输出最大的矩形面积。

Sample Input:

5
6 7 3 8 9
3
2 1 4
Sample Output:

16
4

思路:

暴力可解,对每个木板,分别算出它向左延伸和向右延伸能构成的最长矩形,算出面积,更新ans。

#include <cstdio>#include <cstring>const int maxn = 5000 + 10;int a[maxn];int s[maxn];int main(){    int n;    while(scanf("%d", &n) == 1){        memset(s, 0, sizeof(s));        for(int i = 0; i < n; i++)            scanf("%d", &a[i]);        for(int i = 0; i < n; i++){            for(int j = i; j < n; j++)                if(a[i] <= a[j]) s[i] ++;                else break;            for(int j = i - 1; j >= 0; j--)                if(a[i] <= a[j]) s[i] ++;                else break;            s[i] = a[i] * s[i];        }        int ans = s[0];        for(int i = 1; i < n; i++)            if(s[i] > ans) ans = s[i];        printf("%d\n", ans);    }    return 0;}
0 0
原创粉丝点击