From 2bce2a66afc4c9303380c95e2e7b02cda07eb2b9 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 11 Jul 2025 13:34:41 +0900 Subject: [PATCH] =?UTF-8?q?[20250711]=20BOJ=20/=20G4=20/=20=ED=86=A0?= =?UTF-8?q?=EB=84=88=EB=A8=BC=ED=8A=B8=20=EB=A7=8C=EB=93=A4=EA=B8=B0=20/?= =?UTF-8?q?=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [20250711] BOJ / G4 / 토너먼트 만들기 / 김수연 --- ...0 \353\247\214\353\223\244\352\270\260.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "suyeun84/202507/11 BOJ G4 \355\206\240\353\204\210\353\250\274\355\212\270 \353\247\214\353\223\244\352\270\260.md" diff --git "a/suyeun84/202507/11 BOJ G4 \355\206\240\353\204\210\353\250\274\355\212\270 \353\247\214\353\223\244\352\270\260.md" "b/suyeun84/202507/11 BOJ G4 \355\206\240\353\204\210\353\250\274\355\212\270 \353\247\214\353\223\244\352\270\260.md" new file mode 100644 index 00000000..3f099e73 --- /dev/null +++ "b/suyeun84/202507/11 BOJ G4 \355\206\240\353\204\210\353\250\274\355\212\270 \353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,45 @@ +```java +import java.util.*; +import java.io.*; + +public class boj2262 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + public static void main(String[] args) throws Exception { + nextLine(); + int n = nextInt(); + nextLine(); + List rank = new ArrayList<>(); + for (int i = 0; i < n; i++) rank.add(nextInt()); + int answer = 0; + while (rank.size() > 1) { + int max = -1; + int idx = -1; + + for (int i = 0; i < rank.size(); i++) { + if (rank.get(i) > max) { + max = rank.get(i); + idx = i; + } + } + + int diff; + if (idx == 0) diff = Math.abs(rank.get(idx) - rank.get(idx + 1)); + else if (idx == rank.size() - 1) diff = Math.abs(rank.get(idx) - rank.get(idx - 1)); + else { + int left = Math.abs(rank.get(idx) - rank.get(idx - 1)); + int right = Math.abs(rank.get(idx) - rank.get(idx + 1)); + diff = Math.min(left, right); + } + + answer += diff; + rank.remove(idx); + } + + System.out.println(answer); + } +} +```