超级贪心大王

因为数据是1~n,所以每次输入时判断是否为理想值 (理想情况 n, n-1 ,n-2 , ... ,1 )

如果是就输出、出栈、理想值减一。

如果不是就留下。

最后栈里剩下的只有不能按顺序输出的数了。

#include <iostream>
#include <stack>
using namespace std;

int main() {
    int n,temp,now;
    cin>>n;
    stack<int> st;
    now = n;

    while(n--){
        cin>>temp;
        st.push(temp);
        while(!st.empty() && temp==now){
            cout<<st.top()<<" ";
            now--;
            st.pop();
        }
    }
    while(!st.empty()){
        cout<<st.top()<<" ";
        st.pop();
    }
}