-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_study.cpp
More file actions
43 lines (37 loc) · 847 Bytes
/
06_study.cpp
File metadata and controls
43 lines (37 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
int n, p;
vector <int> chapter;
int max_pages = 0;
int min_pages = 0;
int ans;
bool can_make_group(int mid) {
int group = 1;
int sum = 0;
for (auto c : chapter) {
sum += c;
if (sum > mid) {
group++;
sum = c;
}
}
return group <= p;
}
int main(){
cin >> n >> p;
chapter.resize(n);
for (auto &c : chapter){cin >> c;}
max_pages = accumulate(chapter.begin(), chapter.end(), 0);
min_pages = *max_element(chapter.begin(), chapter.end());
while (min_pages <= max_pages) {
int mid = (min_pages + max_pages) / 2;
if (can_make_group(mid)) {
ans = mid;
max_pages = mid - 1;
} else {
min_pages = mid + 1;
}
}
cout << ans;
return 0;
}