Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views2 pages

Bokkallocation

book allocation solution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Bokkallocation

book allocation solution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include <bits\stdc++.

h>
2 using namespace std;
3
4
5 bool isPossible(int arr[], int n, int m,int mid){
6
7 int studentCount = 1;
8 int pageCount = 0;
9
10 for(int i=0;i<n;i++){
11
12 if(pageCount+arr[i]<=mid){
13 pageCount += arr[i];
14 }
15 else{
16 studentCount++;
17 if(studentCount>m || arr[i]> mid){
18 return false;
19 }
20 pageCount = arr[i];
21 }
22 }
23
24 return true;
25 }
26
27
28 int findPages(int arr[], int n, int m) {
29 // Write your code here.
30
31 int s = 0;
32 int sum = 0;
33
34 for(int i=0;i<n;i++){
35 sum += arr[i];
36 }
37 int e = sum;
38 int ans = -1;
39
40
41 while(s<=e){
42
43 int mid = s + (e-s)/2;
44
45 if(isPossible(arr,n,m,mid)){
46 ans = mid;
47 e = mid-1;
48 }
49 else{
50 s = mid + 1;
51 }
52 }
53 return ans;
54 }
55
56
57
58
59 int main(){
60
61 int size;
62 cin >> size;
63
64 int arr[100];
65
66 for(int i=0;i<size;i++){
67 cin >> arr[i];
68 }
69
70 int n,m;
71 cin >> n>> m;
72
73 cout<<"Solution = "<<findPages(arr,n,m);
74
75 return 0;
76
77
78
79
80 }

You might also like