"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 B.Blind Father【思维枚举】

本文介绍了一种寻找由多个小矩形构成的最大非空子矩阵面积的问题及其解决方案。通过枚举中心点并确定左右边界的方法,实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Blind Father
Time Limit: 1000 MSMemory Limit: 512000 K
Total Submit: 56(21 users)Total Accepted: 20(18 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
样例图形
Source
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛

题目大意:

给你N个小矩形(宽都是1),问你可以找到的最大非空子矩阵的面积是多少。


思路:


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为宽度的矩形。

过程维护最大值即可。

套点思维就可以将时间复杂度降到O(n);

数据比较水,O(n^2)是可过的。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll long long int
ll a[100050];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        ll output=0;
        for(int i=0;i<n;i++)
        {
            int L=i;
            int R=i;
            for(int j=i;j>=0;j--)
            {
                if(a[j]>=a[i])L=j;
                else break;
            }
            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);
    }
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值