Skip to content

Commit accb335

Browse files
authored
[20251123] BOJ / G5 / 기타 레슨
1 parent f985ea4 commit accb335

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static int[] lectures;
9+
private static int N, M, min;
10+
11+
public static void main(String[] args) throws IOException {
12+
init();
13+
int answer = binarySearch();
14+
15+
bw.write(answer + "\n");
16+
bw.flush();
17+
bw.close();
18+
br.close();
19+
}
20+
21+
private static void init() throws IOException {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
N = Integer.parseInt(st.nextToken());
24+
M = Integer.parseInt(st.nextToken());
25+
min = 10000;
26+
27+
lectures = new int[N];
28+
29+
st = new StringTokenizer(br.readLine());
30+
for (int i = 0; i < N; i++) {
31+
lectures[i] = Integer.parseInt(st.nextToken());
32+
min = Math.min(min, lectures[i]);
33+
}
34+
}
35+
36+
private static int binarySearch() {
37+
int left = min;
38+
int right = 1000000000;
39+
int result = 0;
40+
41+
while (left <= right) {
42+
int mid = left + (right - left) / 2;
43+
44+
if (valid(mid)) {
45+
result = mid;
46+
right = mid - 1;
47+
} else {
48+
left = mid + 1;
49+
}
50+
}
51+
52+
return result;
53+
}
54+
55+
private static boolean valid(int target) {
56+
int time = 0;
57+
int count = 1;
58+
59+
for (int lecture : lectures) {
60+
if (lecture > target) {
61+
return false;
62+
}
63+
if (time + lecture <= target) {
64+
time += lecture;
65+
} else {
66+
count++;
67+
time = lecture;
68+
}
69+
}
70+
71+
return count <= M;
72+
}
73+
}
74+
```

0 commit comments

Comments
 (0)