题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080
在加满油的距离下找有没有比当前便宜的,有,就加去第一个比它便宜的地方的油,如果没有,就加满油去剩下的最便宜的站。
注意用一个变量存当前油量。
把终点看成油不要钱的站。
#include <cstdio>
#include <algorithm>
using namespace std;
const double INF = 1e9;
struct station{
double p;
double d;
}P[510];
bool cmp(station a, station b){
if (a.d != b.d)
return a.d < b.d;
else
return a.p < b.p;
}
int main(){
double Cmax, D, Davg;
int N;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
for(int i = 0; i < N; i++)
scanf("%lf%lf", &P[i].p, &P[i].d);
P[N].d = D;
P[N].p = 0;
sort(P, P+N, cmp);
int now = 0;
if(P[now].d){ //这个很坑
printf("The maximum travel distance = 0.00");
return 0;
}
double nowtank = 0, money = 0, maxx = Cmax * Davg, need;
while(now != N){
int min_i = -1;
double min_p = INF;
for(int i = now + 1; i <= N && P[i].d <= P[now].d + maxx; i++){
if(P[i].p < P[now].p){
min_i = i;
need = (P[i].d - P[now].d) / Davg;
if(need > nowtank){
money += (need - nowtank) * P[now].p;
nowtank = 0;
}
else
nowtank -= need;
now = i;
break;
}
if(min_p > P[i].p){
min_i = i;
min_p = P[i].p;
}
}
if(min_i == -1){
money += (Cmax - nowtank) * P[now].p;
break;
}
else if(min_i != now){
money += (Cmax - nowtank) * P[now].p;
need = (P[min_i].d - P[now].d) / Davg;
nowtank = Cmax - need;
now = min_i;
}
}
if(now == N)
printf("%.2f", money);
else
printf("The maximum travel distance = %.2lf", P[now].d + maxx);
return 0;
}