单调栈模板

https://www.acwing.com/problem/content/832/

#include<iostream>
using namespace std;
 
const int N=100010;
int stk[N],tt=0;//tt指向当前栈顶,数据区1开始



int main(){
    int m;
    cin>>m;
    while(m--){
        int x;
        cin>>x;
        while(stk[tt]>=x && tt)   tt--; //将比x大的栈顶数pop
        //剩下的栈顶为空或者有值
        if(tt) cout<<stk[tt]<<" ";
        else cout<<"-1 ";
        //无论找到与否都要入一次栈
        stk[++tt]=x;
    }
}
posted @ 2022-01-07 22:03  秋月桐  阅读(26)  评论(0编辑  收藏  举报