알고리즘
백준 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;
}