forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaintersPartition.cpp
66 lines (62 loc) · 1.64 KB
/
PaintersPartition.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
#include <iostream>
#include <vector>
using namespace std;
bool isFeasible(const vector<int>& boards, int painters, int maxTime){
int painterCount = 1;
int currentBoardSum = 0;
for (int board : boards) {
if (currentBoardSum + board > maxTime) {
painterCount++;
currentBoardSum = board;
if (painterCount > painters || board > maxTime) {
return false;
}
} else {
currentBoardSum += board;
}
}
return true;
}
int MinTimeToPaint(const vector<int>& boards, int painters) {
int n = boards.size();
if (painters > n) {
return -1;
}
int totalSum = 0;
for (int board : boards) {
totalSum += board;
}
int start = 0;
int end = totalSum;
int result = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (isFeasible(boards, painters, mid)) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}
int main() {
int n;
int painters;
cout << "Enter the number of boards: ";
cin >> n;
vector<int> boards(n);
cout << "Enter the lengths of the boards: ";
for (int i = 0; i < n; i++) {
cin >> boards[i];
}
cout << "Enter the number of painters: ";
cin >> painters;
int minTime = MinTimeToPaint(boards, painters);
if (minTime != -1) {
cout << "Minimum time required to paint all boards: " << minTime << endl;
} else {
cout << "Not enough painters to paint all boards." << endl;
}
return 0;
}