-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path10_painter_partition_problem.cpp
147 lines (114 loc) · 4.35 KB
/
10_painter_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Topic - Painter's Partition Problem
Given K painters to paint N boards where each painter takes 1 unit of time to paint 1 unit of boards
i.e. if the length of a particular board is 5, it will take 5 units of time to paint the board.
Compute the minimum amount of time to paint all the boards.
Note that: Every painter can paint only contiguous segments of boards.
A board can only be painted by 1 painter at maximum.
Input Format: First line contains K which is the number of painters.
Second line contains N which indicates the number of boards.
Third line contains N space separated integers representing the length of each board.
Constraints: 1 <= K <= 10
1 <= N <= 10
1<= Length of each Board <= 10^8
Output Format: Output the minimum time required to paint the board.
Sample Input: 2
2
1 10
Sample Output : 10
*/
#include <iostream>
#include <climits>
using namespace std;
// function to check if it is possible to paint all boards in the given time
bool isPossible(int board[], int totalBoard, int totalPainter, int given_time)
{
int time_taken_per_unit = 1; // painter takes 1 unit of time to paint 1 unit of boards
int painter_used = 1;
int time_taken_till_now = 0;
for(int idx=0; idx<= totalBoard-1; idx++)
{
// when board length is greater than allocated time
if(board[idx] > given_time){
return false;
}
// time taken to paint new board
int time_taken_for_new_board = board[idx]*time_taken_per_unit;
if((time_taken_till_now + time_taken_for_new_board) <= given_time )
{
time_taken_till_now += time_taken_for_new_board;
}
else
{
painter_used++;
time_taken_till_now = time_taken_for_new_board;
if(painter_used > totalPainter){
return false;
}
}
}
return true;
}
// function to find minimum time taken by painter's to paint all boards
int getMinTime(int *board, int totalBoard, int totalPainter){
/* Assuming the search space for Number of Minutes &
appling binary search on this time range */
// find sum of all board length
int sum_boards_length = 0;
for(int idx=0; idx<=totalBoard-1; idx++){
sum_boards_length += board[idx] ;
}
// Max time taken by painter (i.e Time take to paint all boards by single painter)
int time_taken_per_unit = 1; // painter takes 1 unit of time to paint 1 unit of boards
int time_taken_to_paint_all_board_by_single_painter = sum_boards_length * time_taken_per_unit;
int start = 1;
int end = time_taken_to_paint_all_board_by_single_painter;
int timeTaken = INT_MAX;
while(start<=end)
{
int mid = (start+end)/2;
// check if it is possible to paint all boards in given time (i.e mid value)
int status = isPossible(board, totalBoard, totalPainter, mid);
if(status){
// if it is possible to paint all boards in x minutes
timeTaken = min(timeTaken, mid); // store minimum value from the eligible time values
end = mid-1;
}else{
// if it is not possible to paint all boards in x minutes
start = mid+1;
}
}
return timeTaken;
}
// function to drive code
int main(){
int testcases;
cout << "Enter total testcases: ";
cin >> testcases;
while(testcases){
int totalPainter, totalBoard;
cout << "Enter number of Painter's & Board: ";
cin >> totalPainter >> totalBoard;
int board[totalBoard];
cout << "Enter length of boards: ";
for(int idx=0; idx<=totalBoard-1; idx++){
cin >> board[idx] ;
}
// finding minimum tme required to paint all boards
int timeTaken = getMinTime(board, totalBoard, totalPainter);
cout << "Minimum time to paint all boards: ";
cout << timeTaken << endl;
testcases--;
}
return 0;
}
/*
OUTPUT:
Enter total testcases: 2
Enter number of Painter's & Board: 2 2
Enter length of boards: 1 10
Minimum time to paint all boards: 10
Enter number of Painter's & Board: 2 3
Enter length of boards: 10 11 10
Minimum time to paint all boards: 21
*/