-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookAllocationProblem.cpp
More file actions
65 lines (65 loc) · 1.61 KB
/
bookAllocationProblem.cpp
File metadata and controls
65 lines (65 loc) · 1.61 KB
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
#include <bits/stdc++.h>
using namespace std;
bool ispossibleSolution(int a[], int n, int m, int sol)
{
int pagesum = 0;
int count = 1;
for (int i = 0; i < n; i++)
{
if (a[i] > sol)
{
return false;
}
if (pagesum + a[i] > sol)
{
count++; // increase the count of student to allocate the next remaining pages
pagesum = a[i];
if (count > m) // if no. of count is greater than students given
{
return false; // condition isi false
}
}
else
{
pagesum += a[i];
}
}
return true;
}
int main()
{
int n, m;
int a[n];
cout << "Enter the Number of Books : ";
cin >> n;
cout << " Enter the Number of Students : ";
cin >> m;
cout << "Enter the Number of Pages per Book : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// Code to find the minimum number of pages
if (m > n)
{ // if number of students is greater than number of books
cout << "Number of Students are Expected less than Number of Books!!!!!!!!!";
return 0;
}
int s = 0;
int end = accumulate(a, a + n, 0); // sum of all pages of array of books
int ans = -1;
while (s <= end)
{
int mid = (s + end) / 2;
if (ispossibleSolution(a, n, m, mid))
{
ans = mid;
end = mid - 1; // if conditiom satisfies the situation
}
else
{
s = mid + 1;
}
}
cout << "Minimum Numbers of Pages to be Allocated is " << ans;
}