-
Notifications
You must be signed in to change notification settings - Fork 13
/
GFG_PaintersPartitionProblem.cpp
70 lines (67 loc) · 1.26 KB
/
GFG_PaintersPartitionProblem.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
70
#include <bits/stdc++.h>
using namespace std;
bool isPossibleSol(int a[], int n, int k, long long mid)
{
long long timeSum = 0;
int cnt = 1;
for (int i = 0; i < n; i++)
{
if (a[i] > mid)
{
return false;
}
if (a[i] + timeSum > mid)
{
cnt++;
timeSum = a[i];
if (cnt > k)
return false;
}
else
{
timeSum += a[i];
}
}
return true;
}
long long minTime(int arr[], int n, int k)
{
// code here
// return minimum time
long long start = 0, end = 0;
for (int i = 0; i < n; i++)
{
end += arr[i];
}
long long ans = -1;
while (start <= end)
{
long long mid = start + (end - start) / 2;
if (isPossibleSol(arr, n, k, mid))
{
ans = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return ans;
}
int main()
{
int t;
cout << "Enter the number of test cases: ";
cin >> t;
while (t--)
{
int k, n;
cin >> k >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
cout << minTime(a, n, k) << endl;
}
return 0;
}