With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

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

思路

  • 把终点视为单位油价为0,离起点距离为 destD 的加油站(以便在最后选择下一站时,能够直接选到终点)
  • 贪心:寻找距离当前加油站最近的油价低于当前油价的加油站,加恰好能够到达该加油站的油,然后前往该加油站;如果找不到油价低于当前油价的加油站,则寻找油价最低的加油站,在当前加油站加满油,然后前往该加油站

注意点

  • 在距离为0处必须有加油站,否则无法出发,一定无法到达终点
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 501;

struct Station {
    double P;
    double D;
} stations[maxn];

int cmp(Station s1, Station s2) {
    return s1.D < s2.D;
}

int main() {
    double capacity, destD, avgD, nowC = 0, nowD = 0, cost = 0;
    int n, nowIndex = 0;
    scanf("%lf %lf %lf %d", &capacity, &destD, &avgD, &n);
    for (int i = 0; i < n; i++) {
        scanf("%lf %lf", &stations[i].P, &stations[i].D);
    }
    stations[n].P = 0;
    stations[n].D = destD;
    sort(stations, stations + n, cmp);
    if (stations[0].D != 0) {
        printf("The maximum travel distance = 0.00");
    } else {
        while (nowD < destD) {
            if (nowIndex == n - 1 && (capacity * avgD < (destD - nowD)) || (nowIndex < n - 1) && (capacity * avgD < (stations[nowIndex + 1].D - nowD))) {
                nowD += capacity * avgD;
                break;
            }
            double minP = stations[nowIndex].P, secondP = stations[nowIndex + 1].P;
            int minIndex = -1, secondIndex = nowIndex + 1;
            for (int i = nowIndex + 1; i <= n; i++) {
                if (capacity * avgD >= (stations[i].D - nowD)) {
                    if (stations[i].P < minP) {
                        minP = stations[i].P;
                        minIndex = i;
                        break;
                    } else if (stations[i].P < secondP) {
                        secondP = stations[i].P;
                        secondIndex = i;
                    }
                } else {
                    break;
                }
            }
            if (minIndex != -1) {
                if (nowC * avgD < (stations[minIndex].D - nowD)) {
                    cost += ((stations[minIndex].D - nowD) / avgD - nowC) * stations[nowIndex].P;
                    nowC = 0;
                } else {
                    nowC -= (stations[minIndex].D - nowD) / avgD;
                }
                nowD = stations[minIndex].D;
                nowIndex = minIndex;
            } else {
                cost += (capacity - nowC) * stations[nowIndex].P;
                nowC = capacity - (stations[secondIndex].D - nowD) / avgD;
                nowD = stations[secondIndex].D;
                nowIndex = secondIndex;
            }
        }
        if (nowD < destD) {
            printf("The maximum travel distance = %.2f", nowD);
        } else {
            printf("%.2f", cost);
        }
    }
    return 0;
}