HDU 1506 单调栈,笛卡尔树,DP

本文探讨了三种解决最大矩形面积问题的高效算法:单调栈、笛卡尔树及线性DP。通过实例代码详细解释了每种算法的实现原理与步骤,为读者提供了深入理解并掌握这些算法的机会。

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

HDU 1506

题目

在这里插入图片描述
如图,求最大的矩形面积。

单调栈

维护一个单调递增的栈。当新进来的数小于前面的,前面的高出部分已经起不到作用了。故在加入新的前,弹出所有高出的元素,统计答案。再加入新的值,把宽度设成刚刚弹出的宽度加1。 O ( n ) O(n) O(n)

#include <bits/stdc++.h>
#define debug(__x) cout<<#__x<<"="<<__x<<endl
#define endl '\n'
using namespace std;
using ll = long long;
using pii = pair<int,ll>;
const int maxn = 1e5+5;
int main(){
    int T;
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n && n){
        stack<pii> s;
        s.push(make_pair(0,-1ll));
        ll h;
        ll ans=0;
        for(int i=1;i<=n+1;++i){
            if(i==n+1) h=0;
            else cin>>h;
            if(s.top().second<h){
                s.push(make_pair(1,h));
            }else{
                int w=0;
                while(s.top().second>=h){
                    pii last = s.top();
                    s.pop();
                    w += last.first;
                    ans = max(ans, w*last.second);
                }
                if(h)s.push(make_pair(w+1,h));
            }
        }
        cout<<ans<<endl;
    }
}

笛卡尔树

很明显了,利用笛卡尔树中序遍历为原序列以及父节点权值小于左右儿子的特点。对笛卡尔树每个节点统计一次答案即可。建树过程由于每个节点都最多加入和弹出一次,所以仍然为 O ( n ) O(n) O(n)

#include <bits/stdc++.h>
#define debug(__x,_ge) cout<<#__x<<"="<<__x<<_ge
#define endl '\n'
using namespace std;
using ll = long long;
using pii = pair<int,ll>;
const int maxn = 1e5+5;
struct Node{
    int idx, val, par, ch[2];
}tree[maxn];
int n,top;
int stk[maxn];
ll ans;
void add(int idx){
    int k=top;
    while(k && tree[stk[k]].val > tree[idx].val)
        --k;
    if(k!=top){  // 不是右链最后一个
        tree[idx].ch[0]=stk[k+1];
        tree[stk[k+1]].par = idx;
    }
    if(k)tree[stk[k]].ch[1] = idx, tree[idx].par = stk[k];
    stk[++k] = idx;
    top = k;
}
int dfs(int u){
    if(!u)return 0;
    int siz = dfs(tree[u].ch[0]) + dfs(tree[u].ch[1]) + 1;
    ans = max(ans, ll(siz) * tree[u].val);
    return siz;
}
int main(){
    int T;
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
    while(cin>>n && n){
        top = 0;
        ans = 0;
        for(int i=1;i<=n;++i){
            cin>>tree[i].val;
            tree[i].idx = i;
            tree[i].par = tree[i].ch[0] = tree[i].ch[1] = 0;
            add(i);
        }
        dfs(stk[1]);
        cout<<ans<<endl;
    }
}
// 7 1 2 3 4 2 1 5 0

线性DP

这是来源于最朴树的思想——枚举每个点为矩形高,左右延申最远的距离为矩形宽。左右延展用到了俩个DP数组。

#include <bits/stdc++.h>
#define debug(__x,_ge) cout<<#__x<<"="<<__x<<_ge
#define endl '\n'
using namespace std;
using ll = long long;
const int maxn = 1e5+5;
int L[maxn],R[maxn];
ll a[maxn];
int main(){
    int n;
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
    while(cin>>n && n){
        for(int i=1;i<=n;++i){
            cin>>a[i];
        }
        for(int k,i=1;i<=n;++i){
            k = i;
            while(k!=1 && a[k-1] >= a[i])
                k = L[k-1];
            L[i] = k;
        }
        for(int k,i=n;i>0;--i){
            k = i;
            while(k!=n && a[k+1] >= a[i])
                k = R[k+1];
            R[i] = k;
        }
        ll ans = 0;
        for(int i=1;i<=n;++i)
            ans  = max(ans, a[i]*(R[i]-L[i]+1));
        cout<<ans<<endl;
    }
}
// 7 1 2 3 4 2 1 5 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值