forked from saumya81/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageAllocation.cpp
69 lines (60 loc) · 1.35 KB
/
PageAllocation.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
60
61
62
63
64
65
66
67
68
69
// Given number of pages in n different books and m students. The books are arranged in ascending order of number of pages.
// Every student is assigned to read some consecutive books.
// The task is to assign books in such a way that the maximum number of pages assigned to a student is minimum.
#include <bits/stdc++.h>
using namespace std;
bool isAllocationPossible(int mid,int a[],int n,int student){
int studentAllocated=1;
int pages=0;
for(int i=0;i<n;i++)
{
if(a[i]>mid)
return false;
if(pages+a[i]>mid)
{
pages=a[i];
studentAllocated+=1;
if(studentAllocated>student)
return false;
}
else{
pages=pages+a[i];
}
}
return true;
}
int allocatepages(int a[],int n,int student)
{
int l=0;
int h=0;
for(int i=0;i<n;i++)
h=h+a[i];
if(l>h)
return -1;
int res = INT_MAX;
while(l<=h){
int mid=(l+h)/2;
if(isAllocationPossible(mid,a,n,student))
{
res=mid;
h=mid-1;
}
else
l=mid+1;
}
return res;
}
int main() {
int n ;
cin>>n;
int a[n];
int h;
for(int i=0;i<n;i++)
{ cin>>a[i];
h=h+a[i];
}
int student;
cin>>student;
int answer= allocatepages(a,n,student);
cout<<answer;
}