PAT-A 1033 To Fill or Not to Fill (25 分)

1033 To Fill or Not to Fill (25 分)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

坑点:杭州市内可能没有加油站。也就是说,第一个加油站距离杭州的距离可能不是0

思路:贪心思想,尽量用最便宜的油跑完两个加油站之间的距离。

油箱大小和单位油耗跑的里程数决定了一辆车加满油可以跑的最远的距离。

先将加油站按照距离杭州的距离从小到大进行排序,然后将目的地的油价设置为0,加入到最后一个站点,这样做是效仿带“哨兵”的插入排序,为了减少特判。

首先判断是否可以续航--如果到下一个加油站的距离大于加满油可以跑的最远距离,那么将无法抵达,输出可以行驶到的最远的距离:当前站点的距离加上汽车可以加满油可以行驶的距离。

如果可以续航,寻找从当前站点到最远可以抵达的距离之间的加油站,遍历这些加油站的油价。如果这些加油站的油价中存在不高于当前站的,找到第一个(注意,贪心思想要求是第一个,而且是不高于,即小于等于)油价不高于当前站的加油站,在当前站加油至刚好可以行驶到那个站点即可(注意终点的油价设置为0,这一步可能直接到终点)。如果没有这样的站,在当前站加满油,行驶到最远可以抵达的加油站。如果当前站是终点,输出最便宜的油费,结束程序。

#include <bits/stdc++.h>

using namespace std;

typedef struct station
{
    double dh;//加油站到杭州的距离
    double price;//油价
}station;

bool cmp(station a, station b)
{   //按照到杭州的距离从小到大排序
    return a.dh < b.dh;
}

station all[505];
int main()
{
    double cmax;//油箱容量,<=100
    double d;//和目标城市的距离<=30000
    double avg;//单位汽油可以跑的距离,<=20
    int n;//加油站的数量,<=500
    
    scanf("%lf %lf %lf %d", &cmax, &d, &avg, &n);
    for(int i = 0; i < n; i++)
        scanf("%lf %lf", &all[i].price, &all[i].dh);

    sort(all, all + n, cmp);//按照距离杭州的距离对加油站进行排序

    all[n].dh = d;//将目的地加入到最后一站,减少特判
    all[n].price = 0;//最后一站的油价必须比所有加油站低

    double tank = 0.0;//油箱初始为空
    double bill = 0.0;//油费
    int now = 0;//当前加油站
    const double dmax = cmax * avg;//加满油最多可以跑的距离
    while(now < n)//当前站为n时到达目的地,退出循环
    {
        //检查杭州是否有加油站
        if(now == 0 && all[now].dh != 0)
        {   //油箱初始为空,第一个加油站不在杭州
            printf("The maximum travel distance = 0.00");
            return 0;
        }
        //检查从当前站是否可以跑到最近的一个加油站
        if(dmax < all[now + 1].dh - all[now].dh)//加满油跑不到最近的站
        {
            printf("The maximum travel distance = %.2f", all[now].dh + dmax);
            return 0;
        }

        //执行到这里,说明还可以续航,计算下一站的位置
        int nextS;//下一站
        bool flag = false;//在当前站加满油可以跑到的站中,是否有比当前站油价低的
        //从当前站出发,在可抵达的范围内,寻找第一个油价不大于当前站的加油站
        for(nextS = now + 1; nextS < n + 1 && all[nextS].dh <= all[now].dh + dmax; nextS++)
        {
            if(all[nextS].price <= all[now].price)
            {   //找到了第一个可抵达且油价不高于的站
                flag = true;
                break;
            }
        }
        if(!flag)//可以抵达的站油价都比当前高
        {
            nextS--;//让nextS指示可以抵达的最远的加油站
            double buy = cmax - tank;//在当前站加满油需要的量
            bill += buy * all[now].price;
            tank = cmax - (all[nextS].dh - all[now].dh) / avg;//到下一站油箱中剩余油量
            now = nextS;//驶向下一站
        }
        else //驶向下一个比当前站油价低的站
        {
            double needtank = (all[nextS].dh - all[now].dh) / avg;//总共需要的油量
            double buy = needtank - tank;//买的油够驶向下一站就好
            bill += buy * all[now].price;//买油花的钱
            tank = 0;//下次加油时油箱为空
            now = nextS;//驶向下一站
        }
    }
    printf("%.2f", bill);//到达终点,输出账单
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值