forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter_partition.cpp
94 lines (86 loc) · 1.97 KB
/
painter_partition.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
86
87
88
89
90
91
92
93
94
/*
You have to paint N boards of length {A0, A1, A2, A3 … AN-1}. There are K
painters available and you are also given how much time a painter takes to
paint 1 unit of board. You have to get this job done as soon as possible
under the constraints that any painter will only paint contiguous sections
of board. Return the ans % 10000003
Input Format
First line contains three space seperated integers N,K and T ,where
N = Size of array,
K = No of painters available ,
T = Time taken to print each board by one painter
Next line contains N space seperated positive integers denoting size of N boards.
Constraints
1<=N<=100000
1<=K<=100000
1<=T<=1000000
1<=Li<=100000
Output Format
Return minimum time required to paint all boards % 10000003.
Sample Input
2 2 5
1 10
Sample Output
50
Explanation
The first painter can paint the first board in 5 units of time and the second painter
will take 50 units of time to paint the second board. Since both can paint simultaneously,
the total time required to paint both the boards is 50.
*/
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
bool painters(int* arr,long long int maxtime,int n,int k)
{
long long int time=0;
int p_used=1;
for(int i=0;i<n;i++)
{
time += arr[i];
if(time > maxtime)
{
time=arr[i];
p_used++;
if(p_used>k)
{
return false;
}
}
}
return true;
}
long int minimum_time(int* arr,int n ,int k,int t,long long int sum)
{
long long int start = *max_element(arr, arr + n);
long long int end = sum;
long long int ans;
while(start<=end)
{
long long mid = (start+end)/2;
if(painters(arr,mid,n,k))
{
ans=mid;
end=mid-1;
}
else
{
start=mid+1;
}
}
return (ans*t)%10000003;
}
int main()
{
int n,k,t;
long long int sum=0;
cin>>n>>k>>t;
int* arr=new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
sum += arr[i];
}
cout<<minimum_time(arr,n,k,t,sum);
delete[] arr;
return 0;
}