1일1알

백준 10972번 다음 순열 C++ 본문

알고리즘

백준 10972번 다음 순열 C++

영춘권의달인 2023. 5. 28. 12:34

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

 

10972번: 다음 순열

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

www.acmicpc.net

 

stl의 next_permutation을 사용하면 쉽게 풀 수 있다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n;
vector<int> v;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    v = vector<int>(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    if (next_permutation(v.begin(), v.end())) {
        for (auto a : v) {
            cout << a << " ";
        }
    }
    else cout << -1;
}

 

'알고리즘' 카테고리의 다른 글

백준 15664번 N과 M(10) C++  (0) 2023.05.30
백준 20365번 블로그2 C++  (0) 2023.05.29
백준 19942번 다이어트 C++  (0) 2023.05.27
백준 14675번 단절점과 단절선 C++  (0) 2023.05.26
백준 21938번 영상처리 C++  (0) 2023.05.25