poj 2559 Largest Rectangle in a Histogram

//题意,给一堆相邻的长方形,求切割出的最大的矩形面积。

//经典的dp

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100005;
int l[maxn], r[maxn], h[maxn];        //分别记录i位置左右高度小于h[i]的最远位置
int main(){
    //freopen("1.txt", "r", stdin);
    int i, n;
    while(scanf("%d", &n)&&n){
        long long ans=0;
        for(i=0; i<n; i++)
        scanf("%d", &h[i]);
        for(i=0; i<n; i++){
            l[i]=i;
            while(l[i]>=1&&h[l[i]-1]>=h[i]){
                l[i]=l[l[i]-1];
            }
        }
        for(i=n-1; i>=0; i--){
            r[i]=i;
            while(r[i]<n-1&&h[r[i]+1]>=h[i]){
                r[i]=r[r[i]+1];
            }
        }
        for(i=0; i<n; i++){
            if((long long)(h[i])*(r[i]-l[i]+1)>ans)
            ans=(long long)(h[i])*(r[i]-l[i]+1);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

poj 2082  Terrible Sets

//几乎一样

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=50005;
int l[maxn], r[maxn], h[maxn], w[maxn], s[maxn];
int main(){
    //freopen("1.txt", "r", stdin);
    int i, n;
    while(scanf("%d", &n)&&n!=-1){
        int ans=0;
        for(i=0; i<n; i++)
        scanf("%d%d", &w[i], &h[i]);
        s[0]=w[0];
        for(i=1; i<n; i++)s[i]=s[i-1]+w[i];
        for(i=0; i<n; i++){
            l[i]=i;
            while(l[i]>=1&&h[l[i]-1]>=h[i]){
                l[i]=l[l[i]-1];
            }
        }
        for(i=n-1; i>=0; i--){
            r[i]=i;
            while(r[i]<n-1&&h[r[i]+1]>=h[i]){
                r[i]=r[r[i]+1];
            }
        }
        for(i=0; i<n; i++){
            if((h[i])*(s[r[i]]-s[l[i]]+w[l[i]])>ans)
            ans=(h[i])*(s[r[i]]-s[l[i]]+w[l[i]]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值