#include <iostream>#include <cstdio>#include <vector>#include <algorithm>usingnamespacestd;// 每次在能达到的范围内油量最大的站加油,注意每停一次后,可达到的范围是动态变化的// 先将所有加油站按照此规则排序// 复杂度: O(n^2)intN,L,P;structstop{intdis;intfuel;};vector<stop>vec;intcmp(conststop&a,conststop&b){if(a.fuel==b.fuel)returna.dis<b.dis;// 因为 dis 是从 town 到加油站的距离,而非起点到加油站的距离returna.fuel>b.fuel;}intget_minimal_stop(){inttotal_fuel=P;vector<stop>::iteratorit;for(intans=0;ans<N;++ans){if(total_fuel>=L)returnans;inttmp=total_fuel;for(it=vec.begin();it!=vec.end();++it){if(total_fuel>=L-it->dis){total_fuel+=it->fuel;vec.erase(it);break;}}if(tmp==total_fuel)// no stop was added and before this: total_fuel < L,// so the town is not reachablereturn-1;}return-1;}intmain(intargc,charconst*argv[]){intdis,fuel;while(scanf("%d",&N)!=EOF){stoptmp;for(inti=0;i<N;++i){scanf("%d%d",&dis,&fuel);tmp.dis=dis;tmp.fuel=fuel;vec.push_back(tmp);}scanf("%d%d",&L,&P);sort(vec.begin(),vec.end(),cmp);cout<<get_minimal_stop()<<endl;}return0;}
//// main.cpp// eoj1855_heap//// Created by whyisyoung on 3/17/14.// Copyright (c) 2014 whyisyoung. All rights reserved.//#include <iostream>#include <cstdio>#include <algorithm>usingnamespacestd;constintmaxn=10005;intN,L,P;structstop{intdis;intfuel;}Stop[maxn];intheap[maxn];intcompare(conststop&a,conststop&b){returna.dis>b.dis;}intmain(){while(~scanf("%d",&N)){for(inti=0;i<N;++i){scanf("%d%d",&Stop[i].dis,&Stop[i].fuel);}scanf("%d%d",&L,&P);sort(Stop,Stop+N,compare);intremain=L-P;intans=0;intsize=0;inti=0;while(remain>0){for(;i<N;++i){// 当前油用完前能经过的加油站入堆if(Stop[i].dis>=remain){heap[size++]=Stop[i].fuel;push_heap(heap,heap+size);}// 若当前加油站不能入堆,则下一个也无法入堆elsebreak;}if(size==0)// 未经过任何一个加油站break;remain-=heap[0];ans++;// 堆顶出栈pop_heap(heap,heap+size);size--;}if(remain>0)cout<<-1<<endl;elsecout<<ans<<endl;}return0;}