解题思路
用数组存所有数,maxn数组存当前项与后面所有项的最大值
然后从左到右依次入栈,如果碰到当前项比后面的项都大,那就出栈打印出来
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[1000100],maxn[1000100],i; //数组一定记得开这么大的,有次提交少个0就错了
cin>>n;
for(i=0;i<n;i++)cin>>a[i];
for(i=n-1;i>=0;i--)maxn[i]=max(maxn[i+1],a[i]); //maxn数组存当前项与后面所有项的最大值 stack<int> st;
for(i=0;i<n;i++)
{
st.push(a[i]); //从左到右依次入栈
while(!st.empty()&&st.top()>maxn[i+1]){cout<<st.top()<<' ';st.pop();} //如果碰到当前项比后面的项都大,那就出栈打印出来
}
while(!st.empty())
{
cout<<st.top()<<' ';
st.pop();
}
}

京公网安备 11010502036488号