using namespace std;
class Comparator {
public:
bool operator()(pair <long int, long int> a, pair <long int, long int> b) {
return a.second < b.second;
}
};
bool comp(pair <long int, long int> a, pair <long int, long int> b)
{
return a.first > b.first;
}
int main()
{
long int tc;
cin >> tc;
long int flag = 0;
while(tc -- )
{
long int n ;
cin >> n ;
vector < pair <long int, long int> > vec;
priority_queue < pair <long int, long int>,vector <pair <long int, long int> >, Comparator > pq;
for(long int i = 0; i < n ; i++)
{
long int x, y;
cin >> x >> y;
vec.push_back({x, y});
}
long int l, p;
cin >> l >> p;
long int ans = 0;
vector <pair <long int, long int> > v;
sort(vec.begin(), vec.end(), comp );
for(long int i = 0 ;i < n ; i++)
{
if(vec[i].first > l)
continue;
while( pq.size()> 0 && p < l && ((l - p) > vec[i].first))
{
pair <long int, long int> t_element = pq.top();
v.push_back(t_element);
pq.pop();
p = p - (l - t_element.first) + t_element.second;
l =t_element.first;
}
pq.push(vec[i]);
}
while ( pq.size()> 0 && l > p && (l- p) <= pq.top().first)
{
pair <long int, long int> t_element = pq.top();
v.push_back(t_element);
pq.pop();
ans++;
p = p - (l - t_element.first) + t_element.second;
l =t_element.first;
}
if(p >= l)
{
cout << v.size() << endl;
}
else
{
cout << -1 << endl;
}
}
return 0 ;
}
I am not able to figure out whats wrong with my approach
~ Approach ~
whenever i find some position that i am not able to reach with the current fuel, i am adding the fuel to the current fuel that i have visited but not included until i am able to reach unreachable position, else i am returning -1
Can anyone provide me testcases where my code fails.