-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (38 loc) · 1.37 KB
/
main.cpp
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
class Solution {
typedef long long ll;
struct Cmp {
bool operator()(pair<int, vector<int>*>& b,
pair<int, vector<int>*>& a) {
if (a.second->at(1) != b.second->at(1)) {
return a.second->at(1) < b.second->at(1);
}
return a.first < b.first;
}
};
public:
vector<int> getOrder(vector<vector<int>>& tasks) {
int n = tasks.size();
vector<pair<int, vector<int>*>> order;
for (int i = 0; i < n; ++i) order.push_back({i, &tasks[i]});
sort(
order.begin(), order.end(),
[](pair<int, vector<int>*>& a, pair<int, vector<int>*>& b) -> bool {
return a.second->at(0) < b.second->at(0);
});
vector<int> result;
priority_queue<pair<int, vector<int>*>, vector<pair<int, vector<int>*>>, Cmp> q;
ll t = 0;
for (int i = 0; i < n || !q.empty();) {
while (i < n && order[i].second->at(0) <= t) q.push(order[i++]);
if (q.empty()) {
int l = order[i].second->at(0);
while (i < n && order[i].second->at(0) == l) q.push(order[i++]);
}
auto& p = q.top();
t = max(t, (ll)p.second->at(0)) + p.second->at(1);
result.push_back(p.first);
q.pop();
}
return result;
}
};