pat甲级1033(25分)c++

pat甲级1033(25分)c++

类型:贪心算法

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

/*
 * 题目大意:
 * 输入:
 * 第一行:
 * Cmax是汽车油箱最大容量,D是总距离,Davg是每单位汽油可以行驶的距离,N是加油站的数量
 * 接下来N行:
 * 每一行:
 * 第一个是每单位汽油的金额,第二个是距离起点的距离
 * 输出:
 * 如果可以到达目的地:输出最小花费金额
 * 如果不可以到达目的地:按格式输出最远行驶距离
 */

const int inf = 99999999;
/*
 * station是加油站的结构体
 * price是每单位汽油的金额,dis是距离起点的距离
 */
struct station {
    double price, dis;
};
/*
 * cmp1是根据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);
    //nowdis是目前行驶距离,maxdis是最大距离,nowprice是目前金额,leftdis是剩余距离
    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
        maxdis = nowdis + cmax * davg;
        double minPriceDis = 0, minPrice = inf;
        //flag判断是否找到比nowprice更低的油价
        int flag = 0;
        //对可以到达的加油站进行遍历
        for(int i = 1; i <= n && sta[i].dis <= maxdis; i++) {
            //如果已经错过,不再考虑
            if(sta[i].dis <= nowdis) continue;
            //如果金额小于nowprice,更新totalPrice
            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;
            }
        }
        //遇到比nowprice高的较低油价,更新totalPrice
        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;
}

本题考查贪心算法,贪心算法是不断寻找尽可能符合条件的情况(个人理解,不一定准确),这道题的思路是不断判断是否存在可到达的加油站,以及在可到达的加油站中进行选择,最优的是价格不断下降且可达,如果价格不是逐渐下降,则找可达加油站中价格最低的,不断重复处理,直到行驶到最远距离,思路一旦清晰,则比较容易处理,但是可能不断需要调试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值