문제 링크입니다 https://www.acmicpc.net/problem/10972
10972번: 다음 순열
첫째 줄에 입력으로 주어진 순열의 다음에 오는 순열을 출력한다. 만약, 사전순으로 마지막에 오는 순열인 경우에는 -1을 출력한다.
www.acmicpc.net
<algorithm> 헤더 파일에 들어있는 next_permutation 함수는 다음 순열을 구해주는 함수입니다. bool type으로 다음 순열이 있으면 true, 없으면 false를 반환하기 때문에 문제에서 주어진 조건에 다음 순열이 없으면 -1을 출력하라는 조건도 어렵지 않게 구현할 수 있습니다.
[소스코드]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
int n;
int main(void) {
scanf("%d", &n);
vector <int> a(n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
if (next_permutation(a.begin(), a.end()))
for (int i = 0; i < n; i++) printf("%d ", a[i]);
else printf("-1");
return 0;
}
|
cs |

'알고리즘 > BaekJoon' 카테고리의 다른 글
백준 6087 : 레이저 통신 (0) | 2020.03.09 |
---|---|
백준 4991 : 로봇 청소기 (0) | 2020.03.09 |
백준 10866 : 덱 (0) | 2020.03.08 |
백준 12851 : 숨바꼭질 2 (0) | 2020.03.08 |
백준 9012 : 괄호 (0) | 2020.03.07 |