#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// boolean function to check if the num
// pages is valid partition or not
bool isValid(ll book[], ll n, ll sum, ll B)
{
// if number of books is less than number of student.
if (n < B)
return false;
else {
// initiailse cur for number of pages
// that student can have.
ll cur = 0;
// initialise number student count for
// assigned number of pages.
ll cnt = 1;
for (ll i = 0; i < n; i++) {
cur += book[i];
// if(cur number of pages is greater than num number
// of pages that one student can have then we incrase
// the number of student by one and start again with
// current number of pages.
if (cur > sum)
{
cnt++;
cur = book[i];
// if number of student is greater than B than
// return false.
if (cnt > B)
return false;
}
}
// if allocation is possible then return true.
if (cnt <= B)
return true;
}
return false;
}
int main()
{
ll t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {
ll n;
cout << "Enter number of books: ";
cin >> n;
ll book[n];
cout << "Enter book pages: ";
for (ll i = 0; i < n; i++)
cin >> book[i];
ll B;
cout << "Enter number of students: ";
cin >> B;
// initialise st for binary search as
// the maximum value among the book.
ll st = *max_element(book, book + n);
// initialisze end for binary search as
// the sum of all the values of books.
ll end = accumulate(book, book + n, 0);
ll ans = INT_MAX;
while (st <= end) {
// find mid of binary search.
ll mid = st + (end - st) / 2;
// check for valid condition then assign the answer.
if (isValid(book, n, mid, B))
{
ans = mid;
end = mid - 1;
}
else
st = mid + 1;
}
if (ans == INT_MAX) {
cout << "Allocation not possible: ";
cout << -1 << "
";
}
else {
cout << "Minimum number of pages: ";
cout << ans << "
";
}
}
return 0;
}
Code Example |
---|
Cpp :: bool nullable to bool c# |
Cpp :: c++ method name |
Cpp :: C++ ss |
Cpp :: Consell de forces polítiques de Catalunya |
Cpp :: what is a .h file in c++ |
Cpp :: HMC 5883 Example to return x y z values |
Cpp :: c++ calling variable constructor |
Cpp :: css window id |
Cpp :: c++ *agrs |
Cpp :: coinPiles |
Cpp :: c++ round number to 2 decimal places |
Cpp :: how does sorting array works in c++ |
Cpp :: how you can add intger value to string in c++ |
Cpp :: The Rating Dilemma codechef solution in c++ |
Cpp :: c++ to mips converter online |
Cpp :: . Shell sort in c++ |
Cpp :: open url from dev cpp |
Cpp :: grepper users assemble |
Cpp :: Link List Insertion a node |
Cpp :: destiny child |
Cpp :: entering char in int c++ avoid loop |
Cpp :: Edmonds-Karp algorithm C++ |
Cpp :: dfs in tree using adjacency list |
Cpp :: C++ (.NET CLI) |
Cpp :: MPI_PUT |
Cpp :: displaying m images m windows opencv c++ |
Cpp :: vector stop at newline |
Cpp :: std::filesystem::path to std::string |
Cpp :: cpp reference array |
Cpp :: c++ ide online |