贪心+优先队列
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> //const int maxn = 1e5+5; #define ll long long #define MAX INT_MAX #define FOR(i,a,b) for( int i = a;i <= b;++i) using namespace std; int N,L,P,ans1,ans,need; struct node { int dis,fuel; }v[11000]; bool cmp(node a,node b) { return a.dis<b.dis; } int main() { priority_queue<int>pque; cin>>N; FOR(i,1,N) { cin>>v[i].dis>>v[i].fuel; } cin>>L>>P; FOR(i,1,N) { v[i].dis=L-v[i].dis; } sort(v+1,v+1+N,cmp); int weizhi=0,youliang=P; v[N+1].dis=L; v[N+1].fuel=0; FOR(i,1,N+1) { need=v[i].dis-weizhi; while(youliang<need) { if(pque.empty()==1) { cout<<"-1"; return 0; } youliang+=pque.top(); pque.pop(); ans++; } pque.push(v[i].fuel); youliang-=need; weizhi=v[i].dis; } cout<<ans<<endl; }