单调栈30pts求助
查看原帖
单调栈30pts求助
549418
hopeless_hope楼主2022/9/20 23:12
#include<iostream>
#include<stack>
using namespace std;
stack<int> s;
int x,k,n,ans;
int main(){
    cin>>n;
    cin>>x;
    s.push(x);
    k=x;
    //cout<<s.size()<<endl;
    for(int i=2;i<=n;i++){
        cin>>x;
        if(x<k){ 
            s.push(x);
            k=x;
        }
        else if(x==k){
            ans++;
            continue;
        } 
        else{
            while(x>s.top()){
                s.pop();
                if(s.empty()){ 
                    s.push(x);
                    k=x;
                    goto akioi;
                }
            }
            s.push(x);
            k=x;
        }
        akioi:
        ans+=(s.size()-1);
    }
    cout<<ans<<endl;
    return 0;
}
2022/9/20 23:12
加载中...