728x90
import sys

N = int(sys.stdin.readline())
data = list(map(int, sys.stdin.readline().split()))

def next_permutation(list_):
    get_i = 0
    for i in range(len(list_)-1):
        if list_[i] < list_[i+1]:
            get_i = i

    get_j = 0
    for j in range(get_i+1, len(list_), 1):
        if list_[get_i] < list_[j]:
            get_j = j

    if get_i == 0 and get_j == 0:
        return False
    else:
        list_[get_i], list_[get_j] = list_[get_j], list_[get_i]
        get_j = len(list_)-1
        while (get_i+1 < get_j):
            list_[get_i+1], list_[get_j] = list_[get_j], list_[get_i+1]
            get_i += 1; get_j -= 1
        return True

if next_permutation(data):
    res = [str(x) for x in data]
    res = ' '.join(res)
    print(res)
else:
    print(-1)
  • 포인트
    • next permutation은 증가조짐인 가장 오른쪽놈(i)와 (i)의 오른쪽놈들 중 (i)보다 큰놈 중 가장 작은 놈(=큰놈 중 가장 오른쪽 놈)(j)을 교체하는 것
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][13023] ABCDE  (0) 2022.05.08
[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10819] 차이를 최대로  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08

+ Recent posts