Skip to content

Commit d2d501d

Browse files
authored
Merge pull request #437 from AlgorithmWithGod/suyeun84
[20250711] BOJ / G4 / 토너먼트 만들기 / 김수연
2 parents c963da9 + 2bce2a6 commit d2d501d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj2262 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
public static void main(String[] args) throws Exception {
12+
nextLine();
13+
int n = nextInt();
14+
nextLine();
15+
List<Integer> rank = new ArrayList<>();
16+
for (int i = 0; i < n; i++) rank.add(nextInt());
17+
int answer = 0;
18+
while (rank.size() > 1) {
19+
int max = -1;
20+
int idx = -1;
21+
22+
for (int i = 0; i < rank.size(); i++) {
23+
if (rank.get(i) > max) {
24+
max = rank.get(i);
25+
idx = i;
26+
}
27+
}
28+
29+
int diff;
30+
if (idx == 0) diff = Math.abs(rank.get(idx) - rank.get(idx + 1));
31+
else if (idx == rank.size() - 1) diff = Math.abs(rank.get(idx) - rank.get(idx - 1));
32+
else {
33+
int left = Math.abs(rank.get(idx) - rank.get(idx - 1));
34+
int right = Math.abs(rank.get(idx) - rank.get(idx + 1));
35+
diff = Math.min(left, right);
36+
}
37+
38+
answer += diff;
39+
rank.remove(idx);
40+
}
41+
42+
System.out.println(answer);
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)