-
Notifications
You must be signed in to change notification settings - Fork 0
/
expedition_greedy_algo.cpp
72 lines (56 loc) · 1.28 KB
/
expedition_greedy_algo.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
bool flag;
while(t--) {
int n;
cin>>n;
vector<pair<int, int>> a(n);
for(int i=0; i<n; i++) {
cin>>a[i].first>>a[i].second;
}
int l, p;
cin>>l>>p;
for(int i=0; i<n; i++) {
a[i].first = l - a[i].first;
}
sort(a.begin(), a.end());
int ans = 0, curr = p;
priority_queue<int, vector<int>> pq;
for(int i=0; i<n; i++) {
if(curr >= l) {
break;
}
while(curr < a[i].first) {
if(pq.empty()) {
flag = 1;
break;
}
ans++;
curr += pq.top();
pq.pop();
}
if(flag) {
break;
}
pq.push(a[i].second);
}
if(flag) {
cout<<"-1"<<endl;
continue;
}
while(!pq.empty() && curr<1) {
curr += pq.top();
pq.pop();
ans++;
}
if(curr < 1) {
cout<<"-1"<<endl;
continue;
}
cout<<ans<<endl;
}
return 0;
}