From 4971cd2471fb307337f08cd350b90d71c8ee8725 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Sat, 15 Nov 2025 22:55:04 +0900 Subject: [PATCH] =?UTF-8?q?[20251115]=20BOJ=20/=20G4=20/=20=EA=B5=AC?= =?UTF-8?q?=EA=B0=84=20=EB=82=98=EB=88=84=EA=B8=B0=202=20/=20=EC=84=A4?= =?UTF-8?q?=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\202\230\353\210\204\352\270\260 2.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" diff --git "a/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" "b/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" new file mode 100644 index 00000000..fa804f69 --- /dev/null +++ "b/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" @@ -0,0 +1,68 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static int[] arr; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + arr = new int[N]; + st = new StringTokenizer(br.readLine()); + + int maxVal = 0; + int minVal = Integer.MAX_VALUE; + + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(st.nextToken()); + maxVal = Math.max(maxVal, arr[i]); + minVal = Math.min(minVal, arr[i]); + } + + int left = 0; + int right = maxVal - minVal; + int answer = right; + + while (left <= right) { + int mid = (left + right) / 2; + + if (canDivide(mid)) { + answer = mid; + right = mid - 1; + } else { + left = mid + 1; + } + } + + System.out.println(answer); + } + + static boolean canDivide(int maxScore) { + int sections = 1; + int minInSection = arr[0]; + int maxInSection = arr[0]; + + for (int i = 1; i < N; i++) { + int newMin = Math.min(minInSection, arr[i]); + int newMax = Math.max(maxInSection, arr[i]); + + if (newMax - newMin > maxScore) { + sections++; + minInSection = arr[i]; + maxInSection = arr[i]; + } else { + minInSection = newMin; + maxInSection = newMax; + } + } + + return sections <= M; + } +} +```