This commit is contained in:
2026-07-29 22:53:24 +05:00
parent 8bc1b22a1c
commit f853068f9b
5 changed files with 146 additions and 15 deletions
+55
View File
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> Range;
typedef pair<int, int> FuelStop;
vector<Range> covered_ranges(const vector<FuelStop>& fuel_stops, const int full_distance, const int initial_fuel) {
vector<Range> ranges{{0, initial_fuel}};
ranges.reserve(fuel_stops.size());
for (const auto& [distance, fuel_amount] : fuel_stops) {
ranges.emplace_back(full_distance - distance, full_distance - distance + fuel_amount);
}
return ranges;
}
int min_stops(vector<Range> ranges, const int full_distance) {
sort(ranges.begin(), ranges.end());
int answer = 0;
int current_pos = 0, max_r = -1;
for (int i = 0; i < ranges.size();) {
if (ranges[i].first <= current_pos) {
max_r = max(max_r, ranges[i].second);
if (max_r >= full_distance) { return answer - 1; }
i++;
} else {
if (max_r < current_pos) { return -1; }
answer++, current_pos = max_r, max_r = -1;
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int fuel_stop_count, full_distance, initial_fuel;
cin >> fuel_stop_count;
vector<FuelStop> fuel_stops(fuel_stop_count);
for (int i = 0; i < fuel_stop_count; i++) {
cin >> fuel_stops[i].first >> fuel_stops[i].second;
}
cin >> full_distance >> initial_fuel;
const vector<Range> ranges = covered_ranges(fuel_stops, full_distance, initial_fuel);
for (const auto& [l, r] : ranges) {
clog << "[" << l << "," << r << "] ";
}
clog << endl;
cout << min_stops(ranges, full_distance) << endl;
return 0;
}