[백준 10819] 차이를 최대로
Algorithm/백준(BOJ)

[백준 10819] 차이를 최대로

반응형

[백준 10819] 차이를 최대로


문제 출처 : https://www.acmicpc.net/problem/10819






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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
 
public class Problem10819 {
    
    static int[] t = new int[27483084]; // static으로 배열 선언
    static int cnt = 0// static으로 int형 변수 선언
    
    public static void swap(int[] arr, int a, int b){ // swap 메소드
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
    
    public static void perm(int[] set, int size, int n, int k){ // 순열 메소드
        
        int sum = 0;
        
        if(size == k){
            for (int i = 0; i < size-1; i++) {
                sum += Math.abs(set[i]-set[i+1]); // 절대값 합 저장
            }
            t[cnt] = sum;
            cnt++;
            return;
        }
        
        for (int i = size; i < n; i++) {
            swap(set, i, size);
            perm(set, size+1, n, k);
            swap(set, i, size);
            
        }
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int count = sc.nextInt();
        int[] arr = new int[count+1];
        int[] set = new int[100];
        
        for (int i = 0; i < count; i++) {
            arr[i] = sc.nextInt();
        }
        
        for (int i = 0; i < count; i++) {
            set[i] = arr[i];
        }
        
        perm(set, 0, count, count);
        
        int max = 0// 최대값 저장
        
        for (int i = 0; i < cnt; i++) {
            if(max <= t[i]) max = t[i];
        }
        
        System.out.println(max);
        
    }
 
}
 
cs


반응형

'Algorithm > 백준(BOJ)' 카테고리의 다른 글

[백준 1057] 토너먼트  (0) 2019.01.23
[백준 8979] 올림픽  (0) 2019.01.20
[백준 1182] 부분집합의 합  (1) 2019.01.17
[백준 1260] DFS와 BFS - 인접 리스트 이용  (0) 2019.01.11
[백준 1018] 체스판 다시 칠하기  (2) 2019.01.10