-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook_Allocation_Problem.cpp
More file actions
executable file
·63 lines (59 loc) · 1.12 KB
/
Book_Allocation_Problem.cpp
File metadata and controls
executable file
·63 lines (59 loc) · 1.12 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
// @author: Abhimanyu Maurya
#include <iostream>
#define ll long long
using namespace std;
//fast i/o
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
bool ot = cout.tie(0);
bool isValid(ll a[], ll n, ll k, ll ans)
{
ll s = 1, b = 0;
for (ll i = 0; i < n; i++)
{
if (a[i] + b <= ans)
{
b += a[i];
}
else
{
b = a[i];
s++;
}
if (s > k or a[i]>ans)
return false;
}
return true;
}
int main()
{
ll n, m, a[100001];
int t;
cin>>t;
while (t--)
{
cin >> n >> m;
ll total = 0, res = 0;
for (ll i = 0; i < n; i++)
{
cin >> a[i];
total += a[i];
}
ll start = a[n - 1], end = total;
while (start <= end)
{
ll mid = (start + end) / 2;
if (isValid(a, n, m, mid))
{
res = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
}
cout << res << "\n";
}
return 0;
}