-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpedition.cpp
56 lines (42 loc) · 1.39 KB
/
expedition.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
#include <bits/stdc++.h>
using namespace std;
#define MAX 10001
typedef pair<int, int> int_pair;
int main() {
int num_cases;
scanf("%d", &num_cases);
while (num_cases--) {
int num_stops;
scanf("%d", &num_stops);
int_pair stops[MAX];
for (int i = 0; i <= num_stops; i++) {
int distance, fuel;
scanf("%d %d", &distance, &fuel);
stops[i].first = distance;
stops[i].second = fuel;
}
int town_distance = stops[num_stops].first;
int curr_fuel = stops[num_stops].second;
for (int i = 0; i < num_stops; i++)
stops[i].first = town_distance - stops[i].first;
sort(stops, stops + num_stops);
priority_queue<int> heap;
bool done = false;
int stop_counter = 0, counter = 0;
while (!done) {
while (counter < num_stops && stops[counter].first <= curr_fuel)
heap.push(stops[counter++].second);
if (curr_fuel >= town_distance) {
done = true;
} else if (heap.empty()) {
stop_counter = -1;
done = true;
} else {
curr_fuel += heap.top();
heap.pop();
stop_counter++;
}
}
printf("%d\n", stop_counter);
}
}