"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛——Hrbust2326 Blind Father

Blind Father
Time Limit: 1000 MS Memory Limit: 512000 K
Total Submit: 42(14 users) Total Accepted: 12(11 users) Rating: Special Judge: No
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
样例图形这里写图片描述

题意:有N块木板垂直于地面,拼接排列成一面,N块木板高度不确定,宽度为1,问如何切割可以得到一个最大的矩形,注意,木板排列顺序和姿势不允许改变【如样例中10 6 10是固定顺序的,因此最大面积为高是6,宽为3的矩形,而不是改变顺序使两块高为10的木板拼接在一起】

暴力搜,遍历每一块木板,以此为标准,大于或等于标准高度的木板被算到宽度总和内,最后标准高×宽度和即为当前最大面积,因为不允许改变顺序,因此都是连续的选择木板。这样每个遍历的木板都向左向右扩展宽度,得出面积,再取最大值即可。

一开始想区间最值和线段树,这样遍历所有木板和区间是会TLE的
考虑到向左向右扩展宽度的方法不一定每次都扩展至边界,是遇到比标准低的高度就停止遍历了,所以时间可能没那么长【当然除非后台数据是一万个相同高度的木板,那就GG了】

#include<stdio.h>
int main()
{
    int n,m,a[10005],i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)///n块木板
        {
            scanf("%d",&a[i]);
        }
        int maxn=0;///初始化面积最大值
        for(i=0;i<n;i++)///从头遍历木板
        {
            int sum=1;///取正在遍历木板本身为1
            for(j=i+1;j<n;j++)///向着木板右边扩展
            {
                if(a[j]>=a[i])///唯有大于等于木板本身的才计数
                {
                    sum++;
                }
                else///一旦发现短于自己的木板,说明最长长度再次截止,直接算面积
                    break;
            }
            for(j=i-1;j>=0;j--)///向木板左边扩展
            {
                if(a[j]>=a[i])
                {
                    sum++;
                }
                else
                    break;
            }
            maxn=sum*a[i]>maxn?sum*a[i]:maxn;///计算面积取最值
        }
        printf("%d
",maxn);///输出最值
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kuronekonano/p/11794359.html