题解 | #栈#

栈和排序

https://ac.nowcoder.com/acm/problem/14893

见代码注释

#include<bits/stdc++.h>
using namespace std;
int a[1000100],r,n;
stack<int>sk;
int main(){
    cin>>n;r=n;
    for(int i=1;i<=n;i++){
        int x;cin>>x;//输入
        sk.push(x);//入栈
        a[x]++;//入栈标记
        while(sk.size()&&r<=sk.top()){//检查当前最大的是否入栈
            while(a[r])r--;//更新最右端
            cout<<sk.top()<<' ';//输出
            sk.pop();//出栈
        }      
    }
}
全部评论

相关推荐

投了200+,0offer,太卷了😭
我的ID配享太庙:今天是92本硕的黄金期 明天是秋招黄金期,咋的还有青铜期白银期,呵呵管他的都是我的死期🥰
点赞 评论 收藏
分享
来来来了1314:查看图片
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务