开始想的随便写的,100%,6000+ms

m = []
n = []
input()
l = list(map(int, input().split()))
for i in l:
    while m:
        if i >= m[-1]:
            if not n:
                m.append(i)
                break
            while n:
                if n[-1]>=i:
                    m.append(i)
                    break
                else:
                    m.append(n[-1])
                    n.pop(-1)
            else:
                m.append(i)
                break
            break
        else:
            n.append(m[-1])
            m.pop(-1)
    else:
        m.append(i)
        continue
    continue
m.extend(n[::-1])
print(*m[::-1])

优化后71.43%

x = []
input()
l = list(map(int, input().split()))
while l:
    top = l[-1]
    l.pop(-1)
    while x and x[-1] > top:
        l.append(x[-1])
        x.pop(-1)
    x.append(top)
print(*x[::-1])