1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <cstdio> #include <algorithm> using namespace std; const int maxn=510; const int INF=1e9; struct Station{ double price,d; }stas[maxn]; double Cmax,dest,aved; int n;
bool cmp(const Station& a,const Station& b){ return a.d<b.d; } int main(){ scanf("%lf%lf%lf%d",&Cmax,&dest,&aved,&n); for(int i=0;i<n;i++){ scanf("%lf%lf",&stas[i].price,&stas[i].d); } sort(stas,stas+n,cmp); stas[n++].d=dest;stas[n].price=0.0; int now=0; double total_d=Cmax*aved; if (stas[0].d!=0.0){ printf("The maximum travel distance = 0.00"); return 0; } double total_price=0.0,nowTank=0.0,MAX=Cmax*aved; while(now<n-1){ double min_price=INF; int ind=-1; for(int i=now+1;i<n;i++){ if(stas[now].d+total_d>=stas[i].d){ if (stas[i].price<min_price){ min_price=stas[i].price; ind=i; if(min_price<stas[now].price){ break; } } } } if (ind==-1) break; double need=(stas[ind].d-stas[now].d)/aved; if (min_price<stas[now].price){ if (nowTank<need){ total_price+=(need-nowTank)*stas[now].price; nowTank=0; }else{ nowTank-=need; } }else{ total_price+=(Cmax-nowTank)*stas[now].price; nowTank=Cmax-need; } now=ind; }
if (now==n-1){ printf("%.2f",total_price); }else{ printf("The maximum travel distance = %.2f",stas[now].d+MAX); } return 0; }
|