forked from TusharKukra/Hacktoberfest2021-EXCLUDED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPainter's_Partition_Problem.cpp
85 lines (72 loc) · 2.08 KB
/
Painter's_Partition_Problem.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Dilpreet wants to paint his dog's home that has n boards with different lengths.
The length of ith board is given by arr[i] where arr[] is an array of n integers.
He hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the board.
The problem is to find the minimum time to get this job done if all painters start together with the
constraint that any painter will only paint continuous boards, say boards numbered {2,3,4} or only board {1} or nothing but not boards {2,4,5}.
*********INPUT************
n = 5
k = 3
arr[] = {5,10,30,20,15}
Output: 35
Explanation: The most optimal way will be:
Painter 1 allocation : {5,10}
Painter 2 allocation : {30}
Painter 3 allocation : {20,15}
Job will be done when all painters finish
i.e. at time = max(5+10, 30, 20+15) = 35
*/
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
bool isvalid(int a[], int n, int k, long long mid){
int p=1;
long long sum=0;
for(int i=0; i<n; i++){
if(a[i]>mid) return false;
sum+=a[i];
if(sum>mid){
p++;
sum=a[i];
}
if(p>k) return false;
}
return true;
}
long long minTime(int a[], int n, int k)
{
long long res=-1;
long long sum=0;
int mx=0;
for(int i=0;i<n; i++){
mx=max(mx,a[i]);
sum+=a[i];
}
long long start=mx, end=sum;
while(start<=end){
long long mid= start + (end-start)/2;
if(isvalid(a,n,k,mid)){
res=mid;
end=mid-1;
}
else start = mid+1;
}
return res;
}
};
// { Driver Code Starts.
int main()
{
int k,n;
cin>>k>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
Solution obj;
cout << obj.minTime(arr, n, k) << endl;
return 0;
} // } Driver Code Ends