오답노트

[순열] BOJ 10973 이전 순열 본문

C,C++/코딩테스트

[순열] BOJ 10973 이전 순열

권멋져 2022. 5. 8. 15:21
https://www.acmicpc.net/problem/10973
 

10973번: 이전 순열

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

www.acmicpc.net

- 문제 파악

주어진 순열의 사전순으로 이전 순열을 출력, 출력할 수 없을 때는 -1을 출력한다.

https://dhjkl123.tistory.com/46

 

STL 순열 관련 함수 ( next_permutation, prev_permutation, rotate)

- next_permutation 오름차순 정렬된 컨테이너를 받는다 입력 컨테이너의 다음 순열을 입력 컨테이너에 입력한다. 다음 순열이 존재하면 true, 존재하지 않으면 false #include // std::cout #include // std::nex..

dhjkl123.tistory.com

 

- 정답

#include "bits/stdc++.h"

using namespace std;

int arr[10001];

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }


    if(prev_permutation(arr, arr+n))
    {
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << " ";
        }
    }
    else
    {
        cout << -1;
    }

}