Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int numberOfRobots, numberOfAbilities, numberOfTasks,
  7.     robots[30], tasks[30], numberOfDependencies;
  8. vector <int> answer, dependency[30], order;
  9. bool used[30];
  10.  
  11. bool ok(int x, int p)
  12. {
  13.     for (int mask = 0; mask < (1 << numberOfTasks); mask ++)
  14.     {
  15.         if (__builtin_popcount(mask) != x) continue;
  16.         int currentRobot = 0;
  17.         for (int j = 0; j < 30; j ++)
  18.         {
  19.             if (mask & (1 << j)) currentRobot |= robots[j];
  20.         }
  21.         if ((currentRobot & tasks[p]) == tasks[p])
  22.         {
  23.             answer.clear();
  24.             for (int j = 0; j < 30; j ++)
  25.             {
  26.                 if (mask & (1 << j)) answer.push_back(j);
  27.             }
  28.             return true;
  29.         }
  30.     }
  31.     return false;
  32. }
  33.  
  34. void sortByDependency(int x)
  35. {
  36.     if (used[x]) return;
  37.     used[x] = true;
  38.     for (int i = 0; i < dependency[x].size(); i ++)
  39.     {
  40.         int to = dependency[x][i];
  41.         sortByDependency(to);
  42.     }
  43.     order.push_back(x);
  44. }
  45.  
  46. int main ()
  47. {
  48.     srand(time(NULL));
  49.     cin >> numberOfRobots >> numberOfAbilities >> numberOfTasks >> numberOfDependencies;
  50.     for (int i = 0, taskA, taskB; i < numberOfDependencies; i ++)
  51.     {
  52.         cin >> taskA >> taskB;
  53.         dependency[taskA].push_back(taskB);
  54.     }
  55.  
  56.     cout << "ORDER BY DEPENDENCY: \n";
  57.     for (int i = 0; i < numberOfTasks; i ++) sortByDependency(i);
  58.     reverse(order.begin(), order.end());
  59.  
  60.     for (int i = 0; i < numberOfTasks; i ++) cout << order[i] << " ";
  61.     cout << "\n";
  62.  
  63.     cout << "ROBOTS: \n";
  64.     for (int i = 0; i < numberOfRobots; i ++)
  65.     {
  66.         cout << i << ": ";
  67.         for (int j = 0; j < numberOfAbilities; j ++)
  68.         {
  69.             int currentRobot = rand() & 1;
  70.             robots[i] |= (currentRobot << j);
  71.             cout << currentRobot << " ";
  72.         }
  73.         cout << "\n";
  74.     }
  75.  
  76.     cout << "\nTASKS: \n";
  77.     for (int i = 0; i < numberOfTasks; i ++)
  78.     {
  79.         cout << i << ": ";
  80.         for (int j = 0; j < numberOfAbilities; j ++)
  81.         {
  82.             int currentRobot = rand() & 1;
  83.             tasks[i] |= (currentRobot << j);
  84.             cout << currentRobot << " ";
  85.         }
  86.         cout << "\n";
  87.     }
  88.  
  89.     for (int i = 0; i < numberOfTasks; i ++)
  90.     {
  91.         ans.clear();
  92.         int currentTask = order[i];
  93.         int l = 0, r = numberOfRobots;
  94.         while (l <= r)
  95.         {
  96.             int mid = (l + r) / 2;
  97.             if (ok(mid, currentTask)) r = mid - 1;
  98.             else l = mid + 1;
  99.         }
  100.         cout << order[i] << ": ";
  101.         if (answer.empty())
  102.         {
  103.             cout << "nobody";
  104.             continue;
  105.         }
  106.         for (int j = 0; j < answer.size(); j ++) cout << answer[j] << " ";
  107.         cout << "\n";
  108.     }
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement