forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpedition-SPOJ_Greedy.cpp
More file actions
81 lines (70 loc) · 1.33 KB
/
Expedition-SPOJ_Greedy.cpp
File metadata and controls
81 lines (70 loc) · 1.33 KB
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
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
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;
int curr = p;
// max heap
priority_queue<int,vector<int> > pq;
bool flag=0;
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<l)
{
curr += pq.top();
pq.pop();
ans++;
}
if(curr<l)
{
cout<<"-1"<<endl;
continue;
}
cout<<ans;
}
return 0;
}