Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<int> getServerIndex(int n, vector<int> arrival, vector<int> burst)
- {
- vector<pair<int, int>> p;
- int sz = arrival.size();
- vector<pair<int, int>> v;
- for (int i = 0; i < sz; i++)
- {
- p.push_back({arrival[i], burst[i]});
- v.push_back({arrival[i], i});
- }
- sort(p.begin(), p.end());
- sort(v.begin(), v.end());
- for (int i = 0; i < sz; i++)
- {
- arrival[i] = p[i].first;
- burst[i] = p[i].second;
- }
- priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> a;
- priority_queue<int, vector<int>, greater<int>> pq;
- for (int i = 1; i <= n; i++)
- pq.push(i);
- vector<int> ans(sz);
- for (int i = 0; i < sz; i++)
- {
- int index = v[i].second;
- int k = arrival[i];
- while (a.size() && a.top().first <= k)
- {
- int free = a.top().second;
- pq.push(free);
- a.pop();
- }
- if (pq.size())
- {
- int time = arrival[i] + burst[i];
- int f = pq.top();
- pq.pop();
- ans[index] = f;
- a.push({time, f});
- }
- else
- {
- ans[index] = -1;
- }
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement