ann8497

OilMines Samsung

Aug 19th, 2019
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. /*
  2. There is an island surrounded by oil mines. You will be given n companies and m oil mines having values. You have to distribute the mines to "n" companies in fair manner. Remember the companies can have oil mines adjacent to each other and not in between of each others.After distributing them compute the difference of oil mines from the company getting highest and company getting lowest. This number should be minimum.(then only the distribution can be termed as fair).
  3.  
  4.   Example
  5.   Input
  6.   2
  7.   2 4
  8.   6 13 10 2
  9.   2 4
  10.   6 10 13 2
  11.  
  12.   output
  13.   5
  14.   1
  15.  
  16. SPECIAL THANKS TO  MY FRINED MOHIT
  17.  
  18. */
  19.  
  20.  
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int ans = 2e9;
  25.  
  26. void findDiff(int comp[], int n) {
  27.     int maxx = -1, minn = 2e9;
  28.     for (int i = 0; i < n; ++i) {
  29.         if (comp[i] < minn) minn = comp[i];
  30.         if (comp[i] > maxx) maxx = comp[i];
  31.     }
  32.     if (maxx - minn < ans) ans = maxx - minn;
  33. }
  34.  
  35. void solve(int curPos, int endPos, int comp[], int mines[], int n, int m, int companyNum, int remainingMines) {
  36.     if (remainingMines < n - companyNum) return;  // remove if company can get 0 mines
  37.     // if all companies have been assigned some mines
  38.     if ((curPos + 1) % m == endPos) {
  39.         // if last mine is remaining and last company is remaining
  40.         if (companyNum == n - 1) {
  41.             // assign last mine to last company and cmpute result
  42.             comp[companyNum] += mines[curPos];
  43.             findDiff(comp, n);
  44.             // backtrack
  45.             comp[companyNum] -= mines[curPos];
  46.         }
  47.         return;
  48.     }
  49.  
  50.     if (companyNum >= n) return;
  51.  
  52.     // assign current mine to current company
  53.     comp[companyNum] += mines[curPos];
  54.     // solve for same company and next mine
  55.     solve((curPos + 1) % m, endPos, comp, mines, n, m, companyNum, remainingMines - 1);
  56.     // solve for next company and next mine
  57.     solve((curPos + 1) % m, endPos, comp, mines, n, m, companyNum + 1, remainingMines - 1);
  58.  
  59.     // do not assign mine to current company (backtracking)
  60.     comp[companyNum] -= mines[curPos];
  61.     // solve for next company and current mine
  62.     if (comp[companyNum] > 0)  // if mine value can't be <= 0 (comment this condition if mine value can be <=0)
  63.         solve(curPos, endPos, comp, mines, n, m, companyNum + 1, remainingMines);
  64. }
  65.  
  66. int main() {
  67.     int n, m;
  68.     // cin >> n >> m;
  69.     n = 3, m = 3;
  70.  
  71.     // mines
  72.     // int mines[m];
  73.     int mines[m] = {1,3,3};
  74.     // for (int i = 0; i < m; ++i) cin >> mines[i];
  75.  
  76.     // comp stores the total mine value assigned to a company
  77.     int comp[n] = {0};
  78.     if (n > m) {  // remove this if a company can get 0 mines
  79.         cout << "-1" << endl;
  80.         return -1;
  81.     }
  82.  
  83.     // start from all the places and solve for all possible ways to fill comp[]
  84.     for (int i = 0; i < m; ++i) {
  85.         solve(i, i, comp, mines, n, m, 0, m);
  86.     }
  87.     cout << ans << endl;
  88.  
  89.     return 0;
  90. }
Add Comment
Please, Sign In to add comment