-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
566ac8d
commit b2f5134
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Searching/Binary Search/Competitive Programming/Book Allocation Problem.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
int findPages(int arr[], int n, int m); | ||
|
||
int main() { | ||
int t; | ||
cin>>t; | ||
while(t--){ | ||
int n; | ||
cin>>n; | ||
int A[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>A[i]; | ||
} | ||
int m; | ||
cin>>m; | ||
cout << findPages(A, n, m) << endl; | ||
} | ||
return 0; | ||
} | ||
|
||
bool solve(int *a, int n, int mid, int m){ | ||
int stu = 1; | ||
int sum = 0; | ||
for(int i=0; i<n; i++){ | ||
if(a[i]>mid) return false; | ||
if(a[i]+sum > mid){ | ||
stu++; | ||
sum = a[i]; | ||
if(stu>m) return false; | ||
} | ||
else sum += a[i]; | ||
} | ||
return true; | ||
} | ||
|
||
int findPages(int *a, int n, int m) { | ||
int lb = 0; | ||
int s = 0; | ||
int ans = 0; | ||
for(int i=0; i<n; i++) s+= a[i]; | ||
int ub = s; | ||
while(lb<=ub){ | ||
int mid = (lb + ub) / 2; | ||
if(solve(a, n, mid, m)){ | ||
ans = mid; | ||
ub = mid - 1; | ||
} | ||
else{ | ||
lb = mid + 1; | ||
} | ||
} | ||
return ans; | ||
} |