距离终点 L,初始油 P。沿途 N 个加油站(距离终点距离、油量)。每单位距离耗 1 油。求最少加油次数到达终点,不能到达输出 -1。
#include#include #include using namespace std; pair st[10100]; int N, P, L; int main() { while (scanf("%d", &N) == 1) { for (int i=0;i pq; int ans = 0, pos = 0, fuel = P; for (int i=0; i<=N; ++i) { int dist = st[i].first - pos; while (fuel < dist) { if (pq.empty()) { ans = -1; break; } fuel += pq.top(); pq.pop(); ++ans; } if (ans == -1) break; fuel -= dist; pos = st[i].first; pq.push(st[i].second); } printf("%d\n", ans); } return 0; }