-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllocateBooks.cpp
59 lines (51 loc) · 1.12 KB
/
AllocateBooks.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
bool check(vector<int> &A, int B , int mid){
int student = 1 ;
int pages = A[0] ;
if(pages > mid){
return false;
}
for(int i = 1 ; i < A.size() ; i++){
pages = pages + A[i];
if(pages > mid){
student++;
pages = A[i];
if(pages > mid ){
return false;
}
}
}
if(student > B){
return false;
}
else{
return true;
}
}
long long int sumofarray(vector<int> &A){
long long int ans = 0 ;
for(int i = 0 ; i < A.size() ;i++){
ans = ans + A[i];
}
return ans;
}
int books(vector<int> &A, int B) {
if(B > A.size()){
return -1 ;
}
long long int start = *min_element(A.begin(),A.end()) ;
long long int end = sumofarray(A);
long long int ans = A[0];
while(start <= end){
long long int mid = (start + end)/2;
if(check(A,B,mid)){
ans = mid ;
end = mid - 1 ;
}
else{
start = mid + 1 ;
}
}
return ans;
}