标签:sort for algo begin define iostream break stat asn
#include <cstdio> #include <cmath> #include <vector> #include <algorithm> #include <climits> #include <unordered_map> #include <cstdio> #include <iostream> # define LL long long using namespace std; struct Station{ double dis; double price; }; int main(){ double cmax, D, davg; int N; scanf("%lf %lf %lf %d", &cmax, &D, &davg, &N); vector<Station> stas(N+1); stas[0].dis=D; stas[0].price=0; for(int i=1;i<=N;i++){ scanf("%lf %lf", &stas[i].price, &stas[i].dis); } sort(stas.begin(),stas.end(),[](Station s1, Station s2){ return s1.dis<s2.dis; }); if(stas[0].dis!=0){ printf("The maximum travel distance = 0.00\n"); return 0; } double curdis=0.0, curprice=stas[0].price, curgas=0.0, maxdis=0.0; double res=0.0; while(curdis<D){ maxdis=curdis+cmax*davg; bool findLow=false; double minprice=99999999; double nextdis=0.0; for(int i=1;i<=N;i++){ if(stas[i].dis<=curdis) continue; if(stas[i].dis>maxdis) break; if(stas[i].price<curprice){ double gasNeeded=(stas[i].dis-curdis)/davg; res+=(gasNeeded-curgas)*curprice; findLow=true; curdis=stas[i].dis; curgas=0; curprice=stas[i].price; break; } if(stas[i].price<=minprice){ minprice=stas[i].price; nextdis=stas[i].dis; } } if(findLow) continue; if(minprice==99999999){ printf("The maximum travel distance = %.2f\n",maxdis); return 0; } res+=(cmax-curgas)*curprice; curgas=cmax-(nextdis-curdis)/davg; curdis=nextdis; curprice=minprice; } printf("%.2f\n",res); return 0; }
标签:sort for algo begin define iostream break stat asn
原文地址:https://www.cnblogs.com/FEIIEF/p/12432451.html