pat 甲级A1033 To Fill or Not to Fill (25分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080

 

题目分析:

题目意思大概是这样的:要求从杭州去往一个目的地,路上有多个加油站,由于不同加油站的油价不同,所有有不同的加油方式,让你选择最便宜的加油方式。如果无法达到目的地,则输出可达到的最远距离。

输入格式,分别给出油箱容量C,杭州距目标城市的距离D,每单位汽油可行驶的里程数Davg,以及加油的数目N。

接着,在下面的N行中,给出该加油站每单位的油价Pi,以及距离杭州的距离Di。

step1,设置加油站结构体,包含油价以及距离杭州的距离信息。

step2,按照距离杭州的距离对加油站进行排序,若第一个加油站的距离不为零,则结束,输出最远距离即为零,因为我们假设开始的时候车辆并没有油。否则进行step3。

step3,设当前汽车到达加油站Di,此时总花费为totP=0,距离杭州的距离为Dis=0,剩余的油量为res=0。

因为要求花费最低,所以我们对油价进行分析:

若Pi 小于等于在该油站加满油可到达最远距离之间所有加油站的油价,那么我们可以知道的是需要在该油站将汽油加满,且达到这些可达到加油站中油价最低的加油站Dj。达到加油站Dj后,更新总花费,距离杭州的距离,以及剩余的油量。

若反之,则将车加到恰好可以去可到达加油站中油价最低的加油站Dj的油量,在到达Dj后,同样更新总花费,距离杭州的距离,以及剩余的油量。

需要注意的是,每次在加油站Di寻找最低油价时,需判断目的地是否在该站可达到的最远距离之内,如果是,直接退出循环,否则继续执行step3。

其实还有一个更统一的形式,直接将目的地也看成一个加油站,然后将该点的油价即为0,根据之前的分析可知,该车最终一定会选择目的地作为可到达的最后一站,且达到目的地后,车的剩余油量一定为0,费用也是最低的。

参考代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 99999999;
struct station {
    double price, dis;
};
bool cmp1(station a, station b) {
    return a.dis < b.dis;
}
int main() {
    double cmax, d, davg;
    int n;
    scanf("%lf%lf%lf%d", &cmax, &d, &davg, &n);
    vector<station> sta(n + 1);
    sta[0] = {0.0, d};
    for(int i = 1; i <= n; i++)
        scanf("%lf%lf", &sta[i].price, &sta[i].dis);
    sort(sta.begin(), sta.end(), cmp1);
    double nowdis = 0.0, maxdis = 0.0, nowprice = 0.0, totalPrice = 0.0, leftdis = 0.0;
    if(sta[0].dis != 0) {
        printf("The maximum travel distance = 0.00");
        return 0;
    } else {
        nowprice = sta[0].price;
    }
    while(nowdis < d) {
        maxdis = nowdis + cmax * davg;
        double minPriceDis = 0, minPrice = inf;
        int flag = 0;
        for(int i = 1; i <= n && sta[i].dis <= maxdis; i++) {
            if(sta[i].dis <= nowdis) continue;
            if(sta[i].price < nowprice) {
                totalPrice += (sta[i].dis - nowdis - leftdis) * nowprice / davg;
                leftdis = 0.0;
                nowprice = sta[i].price;
                nowdis = sta[i].dis;
                flag = 1;
                break;
            }
            if(sta[i].price < minPrice) {
                minPrice = sta[i].price;
                minPriceDis = sta[i].dis;
            }
        }
        if(flag == 0 && minPrice != inf) {
            totalPrice += (nowprice * (cmax - leftdis / davg));
            leftdis = cmax * davg - (minPriceDis - nowdis);
            nowprice = minPrice;
            nowdis = minPriceDis;     
        }
        if(flag == 0 && minPrice == inf) {
            nowdis += cmax * davg;
            printf("The maximum travel distance = %.2f", nowdis);
            return 0;
        }
    }
    printf("%.2f", totalPrice);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值