一二三四五 上山打老虎

单调栈题单-25084Bad Hair Day

链接:https://ac.nowcoder.com/acm/problem/25084

思路:单调栈的裸变式题目,单调递减的单调栈就是计算其右侧能看到的矩形。

代码:

 #include<bits/stdc++.h>

using namespace std;

int main (){
    int n;
    cin>>n;
    vector<int> st(n),wd(n),ve(n+1);
    int ans=0;
    int num,tot=0;
    st[0]=0;
    for(int i=0;i<n;i++)cin>>ve[i];
    ve[n]=0;
    for(int i=0;i<=n;i++){
        num=ve[i];
        if(num>st[tot]){
                st[++tot]=num;
                wd[tot]=1;
        }
        else {
            int w=0;
            while(st[tot]>num){
                w+=wd[tot];
                ans=max(ans,w*st[tot]);
                tot--;
            }
            st[++tot]=num;wd[tot]=w+1;
        }
    }
    cout<<ans;


    return 0;
}

posted @ 2021-04-05 15:00  黒川川  阅读(45)  评论(0)    收藏  举报