반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 순열 알고리즘


[10972번] 다음 순열 :: https://www.acmicpc.net/problem/109732


위의 문제와 동일하다.


이번에는 이전 순열을 찾으면 되는데 이 함수는 prev_permutation이다.

위의 링크에도 많은 설명은 없지만 두 문제의 함수를 챙겨가면 좋을 것 같다.












소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;
 
int main()
{
    int n;
    scanf("%d"&n);
 
    int arr[10001];
    int tmp[10001];
 
    for (int i = 0; i < n; i++)
    {
        scanf("%d"&arr[i]);
        tmp[i] = arr[i];
    }
 
    sort(tmp, tmp + n);
    
    // 사전순으로 가장 처음에 오는 순열인 경우인지 확인
    bool chk = true;
    for (int i = 0; i < n; i++)
        if (tmp[i] != arr[i])
        {
            chk = false;
            break;
        }
 
    if (!chk)
    {
        prev_permutation(arr, arr + n);
 
        for (int i = 0; i < n; i++)
            printf("%d ", arr[i]);
    }
    else
        printf("-1");
    
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1246번] 온라인 판매  (0) 2017.06.20
[1058번] 친구  (0) 2017.06.20
[10972번] 다음 순열  (0) 2017.06.20
[13235번] 팰린드롬 (Palindromes)  (0) 2017.06.20
[6081번] Hay Expenses  (0) 2017.06.20