题解 | #火车进站#
# 定义全局变量,存储出站结果
re = []
def wso(wait, stack, out):
if not wait and not stack: # 没有等待也没有进站的,则已经全部出站out,将结果存储在全局变量re中
re.append(' '.join(map(str, out)))
if wait: # 外面存在等待进站的火车,将排行第一的进站
wso(wait[1:], stack + [wait[0]], out)
if stack: # 车站存有要出站的火车
wso(wait, stack[:-1], out + [stack[-1]])
while True:
try:
n = int(input())
wait = list(map(int, input().split()))
wso(wait, [], [])
for i in sorted(re):
print(i)
except:
break