1033. To Fill or Not to Fill (25)【贪心+模拟】——PAT (Advanced Level) Practise

题目信息

1033. To Fill or Not to Fill (25)

时间限制10 ms
内存限制65536 kB
代码长度限制16000 B

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: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=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: Pi, the unit gas price, and Di (<=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

解题思路

先按距离排序,然后贪心
贪心策略流程:
1. 第一个点不是0,结果为0 (车没法开)结束 。 否则设置当前点为0,然后进入2
2. 按非减序遍历当前点能开到范围内的所有点(提示,注意以下三个顺序,建议直接看代码中注释)
     (1).遇到终点,则加恰好开到终点的油开往终点,结束。
     (2).遇到比当前点便宜的点则加上恰好能开到该点的油开往该点。
     (3).如范围内所有点都比当前点大,则加满油开往范围内价格最小的点。
3. 若未结束,设置当前点为第2步中开往的点,继续第2步。

AC代码

#include <cstdio>
#include <algorithm>
using namespace std;

pair<int, float> station[505];
int Cmax, D, Davg, staNum;

int main()
{
    scanf("%d%d%d%d", &Cmax, &D, &Davg, &staNum);
    for (int i = 0; i < staNum; ++i){
        scanf("%f%d", &station[i].second, &station[i].first);
    }
    sort(station, station + staNum);
    if (station[0].first == 0){
        bool flag = false;
        int curSta = 0, maxDis = 0;
        float price = 0.0f, curC = 0.0f;
        while (true){
            int id = curSta + 1, goodid = -1;
            int maxLimit = station[curSta].first + Cmax * Davg; //能开到最远的地方
            while (id < staNum && station[id].first <= maxLimit){ //范围内寻找站点
                if (station[id].first >= D){ //在找到比当前便宜的站之前能到达终点,直接返回
                    goodid = id;
                    flag = true;
                    break;
                }
                if (goodid == -1 || station[id].second <= station[goodid].second){ //找到第一个站或更优的站
                    goodid = id;
                }
                if (station[id].second <= station[curSta].second){ //找到比当前站点价格便宜的站,直接返回该站
                    goodid = id;
                    break;
                }
                ++id;
            }
            if (flag){ //找到终点
                int len = station[goodid].first - station[curSta].first;
                if (curC * Davg < len){
                    price += 1.0*len/Davg*station[curSta].second;
                }
                printf("%.2f\n", price);
                break;
            }else if (goodid != -1 || maxLimit >= D){ //未找到下一个站点(可能是最后一个点也达不到终点)或范围包含了终点
                if (goodid != -1){ //存在最优点
                    if (maxLimit >= D && station[goodid].second > station[curSta].second){  //能找到终点并且找到的下一个最优点比当前贵,直接加油开向终点
                        flag = true;                
                        int len = D - station[curSta].first;
                        if (curC * Davg < len){
                            price += 1.0*(len-curC*Davg)/Davg*station[curSta].second;
                            curC += 1.0*(len-curC*Davg)/Davg;
                        }
                    }else if (station[goodid].second > station[curSta].second){ //最优点比当前点贵且当前范围探测不到终点
                        price += (Cmax - curC) * station[curSta].second;
                        curC = Cmax;
                    }else{ //最优点比当前点便宜(提醒,终点一定在这个最优点之后,不然会被上面的flag标记截断)
                        int len = station[goodid].first - station[curSta].first;
                        if (curC * Davg < len){
                            price += 1.0*(len-curC*Davg)/Davg*station[curSta].second;
                            curC += 1.0*(len-curC*Davg)/Davg;
                        }
                    }
                }else{ //没找到最优点,即当前为最后一个站点且该站点能开到终点
                    flag = true;
                    int len = D - station[curSta].first;
                    if (curC * Davg < len){
                        price += 1.0*(len-curC*Davg)/Davg*station[curSta].second;
                        curC += 1.0*(len-curC*Davg)/Davg;
                    }
                }
                curC -= 1.0*(station[goodid].first - station[curSta].first)/Davg; //减去路程中消耗的燃料
                curSta = goodid;
                if (flag){
                    printf("%.2f\n", price);
                    break;
                }
            }else{
                printf("The maximum travel distance = %.2f\n", 1.0*maxLimit);
                break;
            }
        }
    }else{
        printf("The maximum travel distance = 0.00\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值