题目大意
解题思路
用栈模拟,如果后面没有比栈顶大的数就弹出栈即可。
code
#include<set>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define fr(i,j) for(int i=begin[j];i;i=next[i])
using namespace std;
int const mn=1e6+9,inf=1e9+7;
int n,a[mn],st[mn],mx[mn];
int max(int x,int y){
return (x>y)?x:y;
}
int read(){
char ch=getchar();
while((ch<'0')||(ch>'9'))ch=getchar();
int v=0;
while((ch>='0')&&(ch<='9'))v=v*10+ch-'0',ch=getchar();
return v;
}
int ww[30];
void write(int x){
ww[0]=0;
while(x)ww[++ww[0]]=x%10,x/=10;
fd(i,ww[0],1)putchar(ww[i]+'0');
putchar(' ');
}
int main(){
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%d",&n);
fo(i,1,n)a[i]=read();
fd(i,n,1)mx[i]=max(mx[i+1],a[i]);
fo(i,1,n-1){
st[++st[0]]=a[i];
while(st[0]&&(mx[i+1]<=st[st[0]]))
write(st[st[0]--]);
}
st[++st[0]]=a[n];
while(st[0])
write(st[st[0]--]);
return 0;
}