Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Seol-JY/202511/15 BOJ G4 구간 나누기 2.md
Original file line number Diff line number Diff line change
@@ -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;
}
}
```