题意:已知某车距离城镇为L,油量为P,油箱无穷大,已知有n个加油量为x的加油站,问,车到城镇最少加几次油。若不能到达城镇,则输出-1。
分析:
1、贪心,先将加油站按照离城镇由远及近排序。
2、卡车只要油够,就不断往前走,若当前油量不足以到达终点(或下一个加油站),则在之前经过的加油站里选择提供油量最大的加油站,加够了再继续走。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int N, L, P;
struct Node{
int d, fuel;
Node(){}
void read(){
scanf("%d%d", &d, &fuel);
}
void Set(int dd, int ff){
d = dd;
fuel = ff;
}
bool operator < (const Node& rhs)const{
return d > rhs.d;
}
}num[MAXN];
priority_queue<int> q;
int solve(){
int ans = 0;
for(int i = 1; i <= N + 1; ++i){
int tmp = num[i - 1].d - num[i].d;
if(P - tmp >= 0) P -= tmp;
else{
while(!q.empty() && P - tmp < 0){
P += q.top();
q.pop();
++ans;
}
if(P - tmp < 0) return -1;
P -= tmp;
}
q.push(num[i].fuel);
}
return ans;
}
int main(){
scanf("%d", &N);
for(int i = 1; i <= N; ++i){
num[i].read();
}
scanf("%d%d", &L, &P);
sort(num + 1, num + 1 + N);
num[0].Set(L, 0);
num[N + 1].Set(0, 0);
printf("%d\n", solve());
return 0;
}