Greedy algorithm
Note two points:
1. Sum with double type
2, the second Test point is the beginning of the situation without a gas station
AC Code
1#include <vector>2#include <cstdio>3#include <algorithm>4#include <map>5 using namespacestd;6 classgas{7 Public:8 Doublep;9 DoubleD;Ten }; One BOOL operator< (Constgas& A,Constgas&b) { A returnA.D <B.D; - } - intMain () { the intC,d,avg,n; -scanf"%d %d%d%d",&c,&d,&avg,&n); - DoubleD (c *avg); -Vector<gas>G; +map<Double,gas>MinG; - for(inti =0; I < n;i++){ + Gas tmp; Ascanf"%LF%LF",&tmp.p,&tmp.d); at if(Ming.find (TMP.D) = = Ming.end () | | MING[TMP.D].P >tmp.p) -MING[TMP.D] =tmp; - } - for(map<Double, Gas>::iterator ite = Ming.begin (); ITE! = Ming.end (); ite++){ -G.push_back (ite->second); - } in //sort (G.begin (), G.end ()); - intCurrentID (0); to floatrem0); + DoubleSum0); - if(g[0].D >0){ theprintf"The maximum travel distance = 0.00\n"); * return 0; $ }Panax Notoginseng for(inti =0; i < g.size (); i++){ - if(I! = G.size ()-1){ the if(G[i +1].D < D && G[i +1].d-g[i].d > D | | G[i +1].D > D && d-g[i].d >D) { +printf"The maximum travel distance =%.2lf\n", G[I].D +D); A return 0; the } + if(G[I].D + D >=d) { - intJ; $ for(j = i +1; J < G.size () && G[J].D < d;j++){ $ if(G[J].P <G[I].P) - Break; - } the if(j = = G.size () | | G[J].P >=G[I].P) { - if(REM >= D-g[i].d)Wuyiprintf"%.2lf\n", sum); the Else -printf"%.2lf\n", Sum + (D-G[I].D-REM)/avg *G[I].P); Wu return 0; - } About } $ intJ; - BOOLS1 (false); - DoubleMinp; - intMiniD; A for(j = i +1; J < G.size () && g[j].d <= g[i].d + d;j++){ + if(J = = i +1|| G[J].P <MINP) { theMINP =G[J].P; -MiniD =J; $ } the if(G[J].P <G[I].P) { the if(Rem < G[J].D-g[i].d) { theSum + = (G[J].D-G[I].D-REM)/avg *G[I].P; theREM =0; -i = J-1; inS1 =true; the Break; the } About } the } the if(S1)Continue; the if(J! =g.size ()) { +Sum + = (D-REM)/avg *G[I].P; -rem = D-(G[MINID].D-g[i].d); thei = MiniD-1;Bayi } the Else{ the if(G[j-1].P <G[I].P) { - if(G[j-1].D-G[I].D >REM) { -Sum + = (G[J-1].D-G[I].D-REM)/avg *G[I].P; theREM =0; thei = J-2; the } the Else{ -REM-= G[j-1].D-g[i].d; thei = J-2; the } the }94 Else{ theSum + = (D-REM)/avg *G[I].P; therem = D-(G[j-1].D-g[i].d); thei = J-2;98 } About } - Continue;101 }102 Else{103 if(G[i].d < D-D) {104printf"The maximum travel distance =%.2lf\n", G[I].D +D); the return 0;106 }107 Else{108 if(REM >= D-g[i].d)109printf"%.2lf\n", sum); the Else111printf"%.2lf\n", Sum + (D-G[I].D-REM)/avg *G[I].P); the return 0;113 } the } the } the}
1033 to fill or not to fill