在一行中输入一个整数
![]()
。
第二行输入
个整数,表示排列
中的元素,用空格分隔。保证给出的是一个从
到
的排列。
输出一行,包含若干整数,表示最终的出栈序列,用空格分隔,结尾不输出多余空格。
5 2 1 5 3 4
5 4 3 1 2
入栈顺序和操作示例如下:2 入栈;
1 入栈;
5 入栈;
5 出栈;
3 入栈;
4 入栈;
4 出栈;
3 出栈;
1 出栈;
2 出栈。
n=int(input())
p=list(map(int,input().split()))
l=p[:]
stack=[]
output=[]
max=sorted(l)[-1]
for i in p:
if i==max:
output.append(i)
l.remove(i)
max=sorted(l)[-1]
else:
stack.append(i)
for j in range(len(stack)):
output.append(stack.pop(-1))
print(' '.join(map(str,output)))