Skip to content

Commit 61c5278

Browse files
committed
[Silver II] Title: 차이를 최대로, Time: 108 ms, Memory: 15300 KB -BaekjoonHub
1 parent 2adca4f commit 61c5278

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver II] 차이를 최대로 - 10819
2+
3+
[문제 링크](https://www.acmicpc.net/problem/10819)
4+
5+
### 성능 요약
6+
7+
메모리: 15300 KB, 시간: 108 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2026년 2월 16일 13:09:54
16+
17+
### 문제 설명
18+
19+
<p>N개의 정수로 이루어진 배열 A가 주어진다. 이때, 배열에 들어있는 정수의 순서를 적절히 바꿔서 다음 식의 최댓값을 구하는 프로그램을 작성하시오.</p>
20+
21+
<p style="text-align:center">|A[0] - A[1]| + |A[1] - A[2]| + ... + |A[N-2] - A[N-1]|</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.</p>
26+
27+
### 출력
28+
29+
<p>첫째 줄에 배열에 들어있는 수의 순서를 적절히 바꿔서 얻을 수 있는 식의 최댓값을 출력한다.</p>
30+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.BufferedReader;
2+
import java.io.FileInputStream;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
private static int answer = 0;
9+
private static int[] arr;
10+
private static int[] current;
11+
private static boolean[] visited;
12+
13+
public static void main(String[] args) throws Exception {
14+
//System.setIn(new FileInputStream("input.txt")); // 제출 시 이 줄만 주석처리
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
int n = Integer.parseInt(br.readLine());
18+
arr = new int[n];
19+
current = new int[n];
20+
visited = new boolean[n];
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
for (int i = 0; i < n; i++) {
23+
arr[i] = Integer.parseInt(st.nextToken());
24+
}
25+
26+
27+
dfs(0);
28+
29+
System.out.println(answer);
30+
}
31+
32+
private static void dfs(int depth) {
33+
if (depth == arr.length) {
34+
//최대 값인지 확인
35+
int sum = 0;
36+
for (int i = 0; i < current.length - 1; i++) {
37+
sum += Math.abs(current[i] - current[i + 1]);
38+
}
39+
40+
answer = Math.max(answer, sum);
41+
return;
42+
}
43+
44+
45+
for (int i = 0; i < arr.length; i++) {
46+
if (visited[i]) continue;
47+
visited[i] = true;
48+
//depth 번째 수를 arr[i]로 선택
49+
current[depth] = arr[i];
50+
dfs(depth + 1);
51+
visited[i] = false;
52+
}
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)