EXPEDI - Expedition
I have attempted this problem using a max heap and sorting . Can you guys spot why my solution get runtime error (SIGABRT) in the eyes of the online judge?
i have run this code for every possible test case(what actually i think) but i didn’t understand why it is getting runtime error. And after i have read the documentation of Error “SIGABRT” but still i didn’t get what actually it means and it is comes up in my code.
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
bool Expendition2(ll& visit) {
ll n;
cin >> n;
vector<pair<ll, ll> > data;
data.push_back({0, 0});
for (ll i = 1; i <= n; i++) {
ll x, y;
cin >> x >> y;
data.push_back({x, y});
}
ll l, p;
cin >> l >> p;
priority_queue<ll> pq;
map<ll, ll> hmap;
for (int i = 1; i <= n; i++) {
data[i].first = (l - data[i].first);
}
data.push_back({l , 0});
sort(data.begin() , data.end());
// for (int i = 0; i <= n + 1; i++) {
// cout << data[i].first << " " << data[i].second << endl;
// }
if (data[1].first > p)
return false;
for (int i = 1; i < n + 1; i++) {
p -= (data[i].first - data[i - 1].first);
pq.push(data[i].second);
// for next stop!
if ( (data[i + 1].first - data[i].first) > p) {
while (!pq.empty()) {
if ((data[i + 1].first - data[i].first) <= p)
break;
p += pq.top();
pq.pop();
visit++;
}
// still problem remains same
if ((data[i + 1].first - data[i].first) > p)
return false;
}
}
}
void Expendition() {
ll t;
cin >> t;
while (t--) {
ll visit = 0;
if (Expendition2(visit)) {
cout << visit << endl;
}
else
cout << -1 << endl;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Expendition();
}