码迷,mamicode.com
首页 > 其他好文 > 详细

问题 C: To Fill or Not to Fill

时间:2020-02-19 19:02:25      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:pac   mes   ble   temp   imu   price   print   ==   lse   


#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;
}

问题 C: To Fill or Not to Fill

标签:pac   mes   ble   temp   imu   price   print   ==   lse   

原文地址:https://www.cnblogs.com/ailinal/p/12332433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!