1033. To Fill or Not to Fill (25)解题报告

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <limits>
#include <cfloat>
#include <cmath>
#define INF 1000000
using namespace std;

struct station {
    double price, dist;
};

bool comp(station s1, station s2);

int main(void) {
    int n, i, j;
    double  cmax, d, davg;
    vector<station> gas_station;
    station tmp;
    scanf("%lf %lf %lf %d", &cmax, &d, &davg, &n);
    for (i = 0; i < n; i++) {
        scanf("%lf %lf", &tmp.price, &tmp.dist);
        gas_station.push_back(tmp);
    }
    sort(gas_station.begin(), gas_station.end(), comp);
    double totalprice = 0.0, remain = 0, present = 0;
    i = 0;
    int index;
    double  min_price;
    if (fabs(gas_station[0].dist - present) > FLT_EPSILON) {
        printf("The maximum travel distance = %.2lf\n", present);
        return 0;
    }
    while (true) {
        min_price = INF;
        index = i + 1;
        for (j = i + 1; j != n && gas_station[j].dist - present <= cmax * davg; j++) {
            if (gas_station[i].price >= gas_station[j].price) {
                min_price = gas_station[j].price;
                index = j;
                break;
            }
        }
        if (min_price > gas_station[i].price) {
            for (j = i + 1; j != n && gas_station[j].dist - present <= cmax * davg; j++) {
                if (min_price > gas_station[j].price) {
                    min_price = gas_station[j].price;
                    index = j;
                }
            }
        }
        if ((index != n && gas_station[index].dist - present > cmax * davg) || (index == n && d - present > cmax * davg)) {
            printf("The maximum travel distance = %.2lf\n", present + cmax * davg);
            break;
        }
        if (min_price > gas_station[i].price) {
            if (cmax * davg >= d - present) {
                totalprice += (d - present) / davg * gas_station[i].price;
                printf("%.2lf\n", totalprice);
                break;
            }
            totalprice += (cmax - remain) * gas_station[i].price;
            remain = cmax;
            remain -= (gas_station[index].dist - present) / davg;
        }
        else {
            if ((gas_station[index].dist - present) / davg - remain > 0) {
                totalprice += ((gas_station[index].dist - present) / davg - remain) * gas_station[i].price;
                remain = 0;
            }
            else {
                remain -= (gas_station[index].dist - present) / davg;
            }
        }
        present = gas_station[index].dist;
        i = index;
    }

    return 0;
}
bool comp(station s1, station s2) {
    return s1.dist < s2.dist;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值