• 7-32 To Fill or Not to Fill (30分)


    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; 

    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

    贪心的思想,本着花钱少的原则,所以要实时记录当前油箱里装的油的油价。首先得按距离排序,不用说都知道当然一个站点一个站点的来。如果说当前加油站的油价更便宜,那就把油加满(注意这里只是假设加满,后面的情况为止,先加满,后面可以调整),如果说油价比油箱里的贵,但是油箱里的油又不够到下个站点,那么仍然要把油箱加满,这里需要特殊注意,邮箱里原本的油相比便宜,加满后邮箱里实际是两种价格的油,我们现在做的是根据策略计算最少花费,所以先计算油箱剩下的油能走多久,然后再更新实时油价,油箱容量也应该空出相应空间,具体看代码可理解。
    代码:
    #include <stdio.h>
    #include <stdlib.h>
    #define inf 0x3f3f3f3f
    struct station {
        double p;
        int d;
    }s[501];
    int cmax,d,davg,n;//最大容量 总距离 每单元油行驶距离
    int cmp(const void *a,const void *b) {
        return ((struct station *)a) -> d - ((struct station *)b) -> d;
    }
    int main() {
        scanf("%d%d%d%d",&cmax,&d,&davg,&n);
        for(int i = 0;i < n;i ++) {
            scanf("%lf%d",&s[i].p,&s[i].d);
        }
        s[n].d = d;
        qsort(s,n,sizeof(s[0]),cmp);
        double mp = inf,sum = 0,tank = 0,md = 0;//油箱里的油价 当前花费 油箱油剩多少 到达的距离
        for(int i = 0;i <= n;i ++) {
            if(davg * tank >= s[i].d - md) {//油箱的油足够到达当前站
                sum += ((s[i].d - md) / davg) * mp;
                tank -= (s[i].d - md) / davg;
                md = s[i].d;
            }
            else {
                md += tank * davg;
                break;
            }
            if(s[i].p <= mp) {//价格低 立即加满
                mp = s[i].p;
                tank = cmax;
            }
            else if(i < n - 1 && (s[i + 1].d - s[i].d) / davg > tank) {//油箱油不够到下一站 价格高也得加满
                md += tank * davg;//剩的油先用完
                sum += tank * mp;//相应花费更新
                mp = s[i].p;
                tank = cmax - tank;//油箱空出相应空间
            }
        }
        if(md < d) printf("The maximum travel distance = %.2f",md);
        else printf("%.2f",sum);
    }
  • 相关阅读:
    python库--pandas--Series.str--字符串处理
    前端--jstree--异步加载数据
    python库--flask--创建嵌套蓝图
    Git--生成公钥和私钥并添加gitlab访问权限
    es查询--请求body
    python生成时间序列(date_range)
    golang使用组合完成伪继承
    golang interface类型的动态绑定
    ruby环境安装草稿
    openfire
  • 原文地址:https://www.cnblogs.com/8023spz/p/12263502.html
Copyright © 2020-2023  润新知