最大的矩形(入门 dp)

Problem Description

在ACdream王国中有一排由N个矩形房子组成的住宅区,每个房子高为h[i],小Z想知道,在这些矩形房子中,你能找到的最大矩形面积是多少呢?
Input

多组数据,对于每组数据,首先是一个整数N,表示房子的数目(1<=N<=100000)
接下来是一行N个整数h[i],表示这些矩形的高度(1<=h[i]<=1000000)
Output

对于每组数据,输出一个整数,表示最大的矩形面积。
Sample Input

5
1 2 3 4 5
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
Sample Output

9
8
4000
Hint

样例一如图:这里写图片描述

就是找以这个为中心,比他高的左右边界,遍历两次。然后再遍历一次找最大值。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
ll h[maxn],l[maxn],r[maxn];
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 1;i <= n;i++)scanf("%lld",&h[i]);
        for(int i = 1;i <= n;i++)
        {
            l[i] = i;r[i] = i;
        }
        for(int i = 2;i <= n;i++)
        {
            if(h[i] > h[i - 1])
                l[i] = i;
            else
            {
                int temp = l[i - 1];
                while(temp >= 1&&h[temp - 1] >= h[i])
                {
                    temp = l[temp - 1];
                }
                l[i] = temp;
            }
        }
        for(int i = n - 1;i >= 1;i--)
        {
            if(h[i] > h[i + 1])
                r[i] = i;
            else
            {
                int temp = r[i + 1];
                while(temp <= n&&h[temp + 1] >= h[i])
                {
                    temp = r[temp + 1];
                }
                r[i] = temp;
            }
        }
        ll ans = 0;
        for(int i = 1;i <= n;i++)
        {
            ans = max(ans,h[i]*(r[i] - l[i] + 1));
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值