单调栈学习记录

单调栈问题
1,单调递增栈(入栈顺序)
https://blog.csdn.net/lucky52529/article/details/89155694
我是从这里学的吼
例题:积水面积(洛谷P1318)

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x,gao;
}a[10500];
stack<node>P;
int n;
int main()
{
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].gao);
            a[i].x=i;
        }
        P.push(a[1]);
        int ans=0;
        for(int i=2;i<=n;i++)
        {
            node past=P.top();
            if(past.gao<=a[i].gao)
            {

                while(!P.empty()&&past.gao<=a[i].gao)
                {
                    int di=past.gao;
                    P.pop();
                    if(P.empty())
                    {
                        break;
                    }
                    past=P.top();
                    ans+=(min(a[i].gao,past.gao)-di)*(a[i].x-past.x-1);
                    //cout<<" di: "<<di<<endl;
                    //cout<<"I: "<<i<<" "<<a[i].gao<<" x: "<<past.x<<" "<<past.gao<<" S: "<<(min(a[i].gao,past.gao)-di)*(a[i].x-past.x-1)<<endl;
                }
               P.push(a[i]);
            }
            else if(past.gao>a[i].gao)
            {
                P.push(a[i]);
            }
        }
        cout<<ans<<endl;
        while(!P.empty())
        {
            P.pop();
        }
    return 0;
}

2,单调递减栈
例题:麻烦的杰西 最大矩形面积
//https://ac.nowcoder.com/acm/contest/3402/B

#include <stack>
#include <iostream>
#include <cstdio>
using namespace std;
struct node
{
    long long int x,gao;
}a[100500];
stack<node>P;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].gao);
            a[i].x=i;
        }
        a[n+1].gao=-1;
        a[n+1].x=n+1;
        P.push(a[1]);
        long long int ans=0;
        long long int h=0;
        if(n==1)
        {
            cout<<a[1].gao<<" "<<a[1].gao<<endl;
            continue;
        }
        for(int i=2;i<=n+1;i++)
        {
            node past=P.top();
            if(P.empty()||a[i].gao>=past.gao)
            {
                P.push(a[i]);
            }
            else
            {
                while(!P.empty()&&P.top().gao>a[i].gao)
                {
                    past=P.top();
                    P.pop();
                    long long int temp=(long long int)(a[i].x-past.x)*past.gao;
                    //cout<<"i: "<<i<<" "<<a[i].gao<<" x: "<<past.x<<" "<<past.gao<<" t: ”<<temp<<endl;
                    if(temp>=ans)
                    {
                        ans=temp;
                        h=past.gao;
                    }
                }
                a[i].x=past.x;
                P.push(a[i]);
            }
        }
        cout<<h<<" "<<ans<<endl;
    }
    return 0;
}```


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值