问题 C: To Fill or Not to Fill

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct station {
    double per_price;
    double distance;
};

bool cmp(const station &s1, const station &s2) {
    return s1.distance < s2.distance;
}

int main() {


    double tank_c, distance, unit_d;
    int n;
    while (scanf(" %lf%lf%lf%d", &tank_c, &distance, &unit_d, &n) != EOF) {

        double cost = 0;
        double current_dis = 0;
        double current_tank = 0;
        double MAX_LEN = tank_c * unit_d;

        station stations[n + 1];
        for (int i = 0; i < n; ++i) {
            scanf(" %lf %lf", &stations[i].per_price, &stations[i].distance);
        }

        station temp = {0, distance};
        stations[n++] = temp;

        sort(stations, stations + n, cmp);

        if (stations[0].distance != 0) {
            printf("The maximum travel distance = 0.00\n");
            break;
        }

        //当前站点
        int k = 0;
        while (current_dis < distance) {
            int nextK = k;
            int min_k = k;
            double min_price = 999;
            double max_distance = MAX_LEN + current_dis;

            if (stations[k + 1].distance - current_dis > MAX_LEN) break;

            bool flag = false;
            for (int i = k + 1; i < n && stations[i].distance <= max_distance; ++i) {
                if (stations[i].per_price < stations[k].per_price) {
                    flag = true;
                    nextK = i;
                    break;
                }
                if (stations[i].per_price < min_price) {
                    min_price = stations[i].per_price;
                    min_k = i;
                }

            }

            double left = current_tank * unit_d;
            //
            if (flag) {

                cost += ((stations[nextK].distance -stations[k].distance - left) / unit_d) * stations[k].per_price;
                k = nextK;
                current_dis = stations[nextK].distance;
                current_tank = 0;
            } else {
                //加满
                cost += ((MAX_LEN -left) / unit_d) * stations[k].per_price;

                current_dis = stations[min_k].distance;
                current_tank = tank_c - (stations[min_k].distance - stations[k].distance)/unit_d;
                k = min_k;
            }


        }


        if (current_dis == distance)
            printf("%.2lf\n", cost);
        else
            printf("The maximum travel distance = %.2lf\n", stations[k].distance + MAX_LEN);


    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值