Advertisement
rajeshinternshala

Untitled

Jan 16th, 2024
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. vector<int> getServerIndex(int n, vector<int> arrival, vector<int> burst)
  2. {
  3.     vector<pair<int, int>> p;
  4.     int sz = arrival.size();
  5.     vector<pair<int, int>> v;
  6.     for (int i = 0; i < sz; i++)
  7.     {
  8.         p.push_back({arrival[i], burst[i]});
  9.         v.push_back({arrival[i], i});
  10.     }
  11.     sort(p.begin(), p.end());
  12.     sort(v.begin(), v.end());
  13.     for (int i = 0; i < sz; i++)
  14.     {
  15.         arrival[i] = p[i].first;
  16.         burst[i] = p[i].second;
  17.     }
  18.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> a;
  19.     priority_queue<int, vector<int>, greater<int>> pq;
  20.     for (int i = 1; i <= n; i++)
  21.         pq.push(i);
  22.     vector<int> ans(sz);
  23.     for (int i = 0; i < sz; i++)
  24.     {
  25.         int index = v[i].second;
  26.         int k = arrival[i];
  27.         while (a.size() && a.top().first <= k)
  28.         {
  29.             int free = a.top().second;
  30.             pq.push(free);
  31.             a.pop();
  32.         }
  33.         if (pq.size())
  34.         {
  35.             int time = arrival[i] + burst[i];
  36.             int f = pq.top();
  37.             pq.pop();
  38.             ans[index] = f;
  39.             a.push({time, f});
  40.         }
  41.         else
  42.         {
  43.             ans[index] = -1;
  44.         }
  45.     }
  46.     return ans;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement