-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPainter Parition.cpp
More file actions
51 lines (43 loc) · 901 Bytes
/
Painter Parition.cpp
File metadata and controls
51 lines (43 loc) · 901 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int minTime(int, int);
int numPainters(int[], int, int);
int main(){
int t, k, n;
cin >> t;
while(t--){
cin >> k >> n;
cout << minTime(n, k) << endl;
}
return 0;
}
int minTime(int n, int k){
int arr[n], l = 0, r = 0, res;
for(int i = 0; i < n; i++){
cin >> arr[i];
r += arr[i];
l = max(l, arr[i]);
}
while(l <= r){
int mid = l + (r - l) / 2;
int paintersRequired = numPainters(arr, n, mid);
if(paintersRequired <= k){
r = mid - 1;
res = mid;
}
else
l = mid + 1;
}
return res;
}
int numPainters(int arr[], int n, int t){
int total = 0, res = 1;
for(int i = 0; i < n; i++){
total += arr[i];
if(total > t){
total = arr[i];
res++;
}
}
return res;
}