This problem was used for: (@ Top coder) Single Round Match 169 Round 1 – Division I, Level Two Single Round Match 169 Round 1 – Division II, Level Three Problem Link
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 |
class FairWorkload{ public: int getMostWork(vector<int> folder,int worker) { int n = folder.size(); int lw = *max_element(folder.begin(),folder.end()); int hi = accumulate(folder.begin(),folder.end(),0); int mid_load; int req_workers=1; int cur_load; if(worker==1) return hi; while(lw<hi) { mid_load = (lw+hi)/2; cur_load=0; req_workers=1; for(int i=0;i<n;i++) { if(cur_load+folder[i]<=mid_load) { cur_load += folder[i]; } else { cur_load=folder[i]; req_workers += 1; } } if(req_workers<=worker) hi = mid_load; else if(req_workers>worker) lw=mid_load+1; } return hi; } }; |
Total solution to run and test as a single program.
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 |
#include <iostream> #include <cstdio> #include <vector> #include <numeric> #include <algorithm> using namespace std; vector<int> folder; class FairWorkload{ public: int getMostWork(vector<int> folder,int worker) { int n = folder.size(); int lw = *max_element(folder.begin(),folder.end()); int hi = accumulate(folder.begin(),folder.end(),0); int mid_load; int req_workers=1; int cur_load; if(worker==1) return hi; while(lw<hi) { mid_load = (lw+hi)/2; cur_load=0; req_workers=1; for(int i=0;i<n;i++) { if(cur_load+folder[i]<=mid_load) { cur_load += folder[i]; } else { cur_load=folder[i]; req_workers += 1; } } if(req_workers<=worker) hi = mid_load; else if(req_workers>worker) lw=mid_load+1; } return hi; } }; int main() { int a,workers,n; while(1) { cout<<"enter no of file cabinets: "; cin>>n; folder.clear(); for(int i=0;i<n;i++) { cin>>a; folder.push_back(a); } cin>>workers; FairWorkload fair; cout<<fair.getMostWork(folder,workers)<<"\n"; } return 0; } |