主要考察优先队列,思路:
1.以汽车为原地,建立一维坐标系。对加油站的坐标从小到大排序。
2.经过一个加油站,就把加油站的油量压入优先队列中,没油的时候就取出一个最大的,队列为空的时候意味着不能到达终点。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
#define MAX 10005
using namespace std;
struct node {
int stop;
int fuel;
};
node a[MAX];
bool com(node a, node b)
{
return a.stop < b.stop;
}
int main()
{
int N; cin >> N;
for (int i = 0; i < N; i++)
cin >> a[i].stop >> a[i].fuel;
int L, P;
cin >> L >> P;
for (int i = 0; i < N; i++)
a[i].stop = L - a[i].stop;//以车起点为原点
a[N].stop = L;//终点坐标
sort(a, a + N, com);//对加油站坐标进行排序
priority_queue<int>que;
int pos = 0, tank = P, ans = 0;
for (int i = 0; i <= N; i++)
{
int d = a[i].stop - pos;//接下来要走的距离
while (tank < d)
{
if (que.empty())
{
cout << -1; return 0;
}
tank += que.top(); que.pop();
ans++;
}
pos = a[i].stop;
tank -= d;
que.push(a[i].fuel);
}
cout << ans;
system("pause");
}