【ACM】PAT. A1033 To Fill or Not to Fill【贪心算法】

题目链接
题目分析

(略)

解题思路

标记终点:把终点看做是距离为总距离Dis,油价为0的站点

  • 策略1:在满油能到达的站中选择 油价比本站低的 最近站点
  • 策略2:若满油能到达的站中无 油价比本站低的站点,
    则在可到达的点中选择 油价最低 的
  • 策略3:满油仍无可到达站点 或 到达终点 时退出

AC程序(C++)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxSize 1010

using namespace std;

struct station {
    double price;
    double dis; //距起点的距离
    bool operator < (const station & A)const {//操作符重载
        return dis < A.dis;
    }
}st[maxSize];

void beGreedy(int cap, int Dis,int av, int n) {
    double sumPrice = 0; //总花费
    double curDis = 0; //当前已走路程
    double curGas = 0; //油箱中剩余油量
    double maxDis = cap * av; //满油状态下能走的最远距离

    //出发点必须有加油站,否则无法出发
    if (st[0].dis != 0) {
        cout << "The maximum travel distance = 0.00" << endl;
        return;
    }

    for (int i = 0; i < n; ) {
        double needDis; //到下一个站的距离
        int nextSt = -1; //下一站的序号
        int minPrice_next = i + 1; //记录可选站点中油价最低的
        int j, flag = 0;

        for (j = i + 1; j <= n; j++) {//寻找下一站
            needDis = st[j].dis - curDis;

            if (maxDis < needDis) { break; }
        //满油也无法到达此站点【因为按距离排序,后边站点不用考察】
            else if(st[j].price < st[i].price) {//满足策略1
                nextSt = j;//记录下一站
                flag = 1;//记录有站点可达
                break;
            }
            //等号一定要加
            else if(st[j].price <= st[minPrice_next].price){
                flag = 1;//记录有站点可达
                minPrice_next = j; //记录 油价最低的站点
            }
        }//for - j

        if (flag == 0) { //满油也无法到达 最近站点
            double temp = curDis + maxDis;
            printf("The maximum travel distance = %.2lf\n", temp);
            return;
        }
        if (nextSt == n) {//到达终点
            //由于终点油价为0,故不可能minPrice_next == n
            needDis = Dis - curDis;
            sumPrice += ((needDis/av - curGas) * st[i].price);
            printf("%.2lf\n", sumPrice);
            return;
        }
        if (nextSt == -1) {//满足策略2  加满油箱
            nextSt = minPrice_next;
            needDis = st[nextSt].dis - curDis;
            sumPrice += ((cap - curGas) * st[i].price); //油箱加满
            curGas = cap - (needDis / av); //剩余油量
            curDis += needDis;//前进
        }
        else {//满足策略1, 购买刚好到下一站的油即可【注:用本站油价】
            needDis = st[nextSt].dis - curDis;
            sumPrice += ((needDis/av - curGas) * st[i].price);  
            curDis += needDis;  //前进
            curGas = 0; //油箱无剩余
        }

        i = nextSt; //序号 变为 下一站点
    }//for - i
}


int main() {
    double cap, Dis, av;  //油箱容量, 总距离, km/gas
    int n;  // 加油站数量
    cin >> cap >> Dis >> av >> n;
    for (int i = 0; i < n; i++) {
        cin >> st[i].price >> st[i].dis;
    }
    //把终点看做是距离为Dis,油价为0的站点
    st[n].price = 0;
    st[n].dis = Dis;
    sort(st, st + n + 1);//按距离远近排序
    beGreedy(cap, Dis, av, n);//贪心选择

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值