#include <iostream>
#include<stack>
#include<vector>
using namespace std;
//如果x大于栈内的最大值,则它适合直接输出
//如果x不大于栈内最大值,则不输出
//所有元素进入栈内后,输出剩余元素
int main() {
    int n;
    cin>>n;
    int max=n;
    int shuzu[n];
    stack<int> arr;
    for(int i=1;i<=n;i++)
    {
        cin>>shuzu[i];
    }
    for(int i=1;i<=n;i++)
    {
        arr.push(shuzu[i]);
        if(shuzu[i]==max)
        {
            cout<<arr.top()<<" ";
            arr.pop();
            max--;
        }
    }
    while(!arr.empty())
    {
        cout<<arr.top()<<" ";
        arr.pop();
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

冰箱关大象,仔细分析每一步的选择和条件判断。

题目案例里面给了很好的思路引导

字典序就是把最大的最先输出,其余的不管