Test instructions: A truck away from the key L, the existing oil volume p, the truck every 1 meters of oil consumption 1, on the way there are some gas stations, ask at least at a few gas stations refueling can make the truck reach the end or reach the end. Idea: Use priority queue, will be able to go to the gas station to join the priority queue, the oil is not enough when the priority queue to join the highest value of oil, if the oil is not enough when the queue empty can not reach.
#include <cstdio>#include<queue>#include<algorithm>using namespaceStd;priority_queue<int>que;structStop {intDis,fuel;} stop[10010];BOOLCMP (Stop a,stop b) {if(A.dis==b.dis)returnA.fuel>B.fuel; returna.dis<B.dis;}intMain () {intn,l,p; while(~SCANF ("%d",&N)) { for(intI=0; i<n;i++) {scanf ("%d%d",&stop[i].dis,&Stop[i].fuel); } scanf ("%d%d",&l,&p); for(intI=0; i<n;i++) {Stop[i].dis=l-Stop[i].dis; } sort (Stop,stop+n,cmp); Stop[n].dis=l;stop[n].fuel=0; intCnt=0, pos=0, flag=0; for(intI=0; i<=n;i++) { intd=stop[i].dis-POS; while(p<d) {if(!Que.empty ()) {P+=Que.top (); Que.pop (); CNT++; }Else Break; } if(p<d) {flag=1; Puts ("-1"); Break; } P-=D; Que.push (Stop[i].fuel); POS=Stop[i].dis; } if(flag==0) printf ("%d\n", CNT); while(!que.empty ()) Que.pop (); } return 0;}
POJ 2431 greedy + priority queue