forked from DionysiosB/CodeForces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
803D-MagazineAd.cpp
43 lines (34 loc) · 962 Bytes
/
803D-MagazineAd.cpp
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 <cstdio>
#include <iostream>
#include <vector>
bool check(std::vector<long> a, long maxWidth, long maxLength){
long w(0), length(0);
for(long p = 0; p < a.size(); p++){
if(a[p] > maxWidth){return false;}
if(length > maxLength){return false;}
if(w + a[p] > maxWidth){++length; w = 0;}
w += a[p];
}
if(w > 0){++length;}
return (length <= maxLength);
}
int main(){
long k; std::string s;
scanf("%ld\n", &k); getline(std::cin, s);
std::vector<long> a;
long last(-1);
for(long p = 0; p < s.size(); p++){
if(s[p] != ' ' && s[p] != '-'){continue;}
a.push_back(p - last);
last = p;
}
a.push_back(s.size() - last - 1);
long left(0), right(s.size() + 1);
while(left <= right){
long mid = (left + right) / 2;
if(check(a, mid, k)){right = mid - 1;}
else{left = mid + 1;}
}
printf("%ld\n", left);
return 0;
}