Algorithm/백준

[백준] 10973 이전순열

sunnyshiny 2023. 3. 17. 20:10
728x90

https://www.acmicpc.net/problem/10973

 

10973번: 이전 순열

첫째 줄에 입력으로 주어진 순열의 이전에 오는 순열을 출력한다. 만약, 사전순으로 가장 처음에 오는 순열인 경우에는 -1을 출력한다.

www.acmicpc.net

 

n = int(input())
li = list(map(int, input().split()))

for i in range(n-1, 0, -1):
    if li[i] < li[i-1]:
        x = i-1
        for j in range(n-1, 0, -1):
            if li[x] > li[j]:
                li[x], li[j] = li[j], li[x]
                li = li[:x+1] + sorted(li[x+1:], reverse=True)
                print(*li)
                exit()
print(-1)
728x90