CS/알고리즘_문제풀이(자바)

차이를 최대로

Jedy_Kim 2021. 10. 4. 12:51
728x90

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

 

10819번: 차이를 최대로

첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.

www.acmicpc.net

// 코드

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
import java.util.*;
import java.io.*;
 
public class Main{  
 
  static int N;
  static int max;
  static int[] arr; 
  static int[] result;
  static int[] check;
  
  public static void getResult(int x) {
    if(x >= N) {
      int sum = 0;
      for(int i=0; i<N-1; i++) sum += Math.abs((result[i] - result[i+1]));
      max = Math.max(max, sum);
    } else {
      
      for(int i=0; i<N; i++) {
        if(check[i] == 0) {
          check[i] = 1;
          result[x] = arr[i];
          getResult(x+1);
          check[i] = 0;
        }
      }
      
    }
  }
  
  // main
  public static void main(String[] args) throws Exception {
    
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    
    N      = Integer.parseInt(br.readLine());
    check  = new int[N];
    result = new int[N];
    arr    = new int[N];
    arr    = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    
    Arrays.sort(arr);
    max = 0;
    getResult(0);
    
    bw.write(String.valueOf(max));
    br.close();
    bw.flush();
    bw.close();
  }
}
cs
반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

N과 M (7)  (0) 2021.10.05
N과 M (8)  (0) 2021.10.05
모든 순열  (0) 2021.10.04
중복 없는 구간  (0) 2021.10.03
2차식 정답 추측  (0) 2021.10.03