File tree Expand file tree Collapse file tree 1 file changed +74
-0
lines changed
Expand file tree Collapse file tree 1 file changed +74
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments