-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_extended.cpp
43 lines (41 loc) · 1.04 KB
/
binary_search_extended.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class FairWorkLoad {
public:
int getMostWork(vector<int> folders, int workers) {
int n = folders.size();
int lo = *max_element(folders.begin(), folders.end());
int hi = accumulate(folders.begin(), folders.end(), 0);
while(lo < hi) {
int required = 1;
int x = lo + (hi - lo) / 2;
int current_load = 0;
for(int i = 0; i < n; ++i) {
if(current_load + folders[i] <= x) {
current_load += folders[i];
} else {
++required;
current_load = folders[i];
}
}
if(required <= workers) {
hi = x;
} else {
lo = x + 1;
}
}
return lo;
}
};
int main(int argc, char const *argv[])
{
vector<int> folders { 568, 712, 412, 231, 241, 393, 865, 287, 128, 457, 238, 98, 980, 23, 782 };
int workers = 4;
int maxFairLoad;
FairWorkLoad *fw = new FairWorkLoad();
maxFairLoad = fw->getMostWork(folders, workers);
cout << maxFairLoad << endl;
return 0;
}