image.png

/*
Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00
*/
#include<cstdio>
#include<algorithm>
const int maxn = 510;
const int INF = 999999999;
using namespace std;
int N;
double cmax, D, Davg;
struct station {
    double p = 0;
    double d = 0;
} station[maxn];
bool cmp(struct station a, struct station b) {
    return a.d < b.d;
}
int main() {
    scanf("%lf%lf%lf%d", &cmax, &D, &Davg, &N);
    for (int i = 0; i < N; i++) {
        scanf("%lf%lf", &station[i].p, &station[i].d);
    }
    station[N].d = D;
    station[N].p = 0;
    sort(station, station + N, cmp);
    if (station[0].d != 0)
        printf("The maximum travel distance = 0.00\n");
    else {
        int now = 0;//现在的站点;
        double cost = 0;//总花费
        double nowTank = 0;//当前油箱油量
        double MAX = cmax * Davg;//最远行走距离
        while (now < N) {
            //先进行最小站点选择
            int next_station = -1;
            double min_price = INF;
            for (int i = now + 1; i <= N && station[i].d - station[now].d <= MAX; i++) {
                if (station[i].p < min_price) {
                    min_price = station[i].p;
                    next_station = i;
                    if (station[i].p < station[now].p) {
                        //如果小于当前起点的油价,直接退出
                        break;
                    }
                }
            }
            if (next_station == -1)
                break;//没有找到下一站
            double need = (station[next_station].d - station[now].d) / Davg;//到下一站所要的油量
            if (min_price < station[now].p) {
                //如果低于当前站点的中继站
                if (nowTank < need) {
                    //如果当前油量不能到达下一站
                    cost += (need - nowTank) * station[now].p;
                    nowTank = 0;
                } else
                    nowTank -= need;
            } else {
                //到了更远的一战,当前站的油更便宜,直接加满
                cost += (cmax - nowTank) * station[now].p;
                nowTank = cmax - need;
            }
            now = next_station;
        }
        if (now == N)
            printf("%.2f\n", cost);
        else
            printf("The maximum travel distance = %.2f\n", station[now].d + MAX);
    }
    return 0;
}

Tassadar
0 声望1 粉丝

我只想安静地做个笔记