zoukankan      html  css  js  c++  java
  • PAT A1033 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: 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 Nlines 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
    
     
    #include <stdio.h>
    #include <string>
    #include <algorithm>
    using namespace std;
    float f[30010] = { -1 };
    int main() {
        fill(f, f + 30010, -1.0);
        int cmax, d, da, n, flag = 0, out = -1;
        scanf("%d %d %d %d", &cmax, &d, &da, &n);
        for (int i = 0; i < n; i++) {
            int dis;
            float p;
            scanf("%f %d", &p, &dis);
            if(dis<d)f[dis] = p;
        }
        int now = 0;
        float total = 0;
        float need = 0, remain = 0;
        int can = da * cmax;
        if (f[0] == -1) {
            printf("The maximum travel distance = %d.00", now);
            return 0;
        }
        while (now < d) {
            int i, flag1 = 0, flag2 = 0;
            float min = 99999999.0;
            for (i = 1; i <= can; i++) {
                if(now+i<=d){
                    if (f[i + now] != -1) {
                        if (f[i + now] <= f[now]) {
                            flag2 = i;
                            break;
                        }
                        else {
                            if (f[i + now] < min) {
                                flag1 = i;
                                min = f[i + now];
                            }
                        }
                    }
                }
                else {
                    flag1 = 0;
                    break;
                }
            }
            if (flag1 == 0 && flag2 == 0 && now + can < d) {
                now += can;
                printf("The maximum travel distance = %d.00", now);
                system("pause");
                return 0;
            }
            else if (flag2 != 0) {
                need = flag2 - remain;
                remain = 0;
                total += need * f[now]/da;
                now += flag2;
            }
            else if (flag1 != 0) {
                need = cmax * da - remain;
                total += need * f[now]/da;
                remain = cmax * da - flag1;
                now += flag1;
            }
            else {
                total += f[now] * (d - now)/da;
                now = d;
            }
        }
        printf("%.2f", total);
        system("pause");
        return 0;
    }

    注意点:一道贪心算法题,有点麻烦,分几种情况,

    1、能到最大距离里没有加油站的

    2、能到的加油站比现在的便宜,就只加到那里的油

    3、能到的加油站都比现在的贵,就要加满油箱

    4、能到距离没加油站但到终点了

    其中第三点贪心要注意,一开始没考虑,怎么算都不对。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    Selenium 3 + BrowserMobProxy 2.1.4 模拟浏览器访问 (含趟坑)
    macOS Sierra WiFi connecting problem
    Accumulator<Long> implements of JavaSparkContext in Spark1.x
    写了一个Android动画的启动界面
    用C#简单实现了数据的封装
    关于JAVA数据结构中的栈操作
    写了一个关于将XML文件导入数据库的程序(C#,sql server)
    经典电影里的数学应用
    初步学习多线程操作,代码不是完美的,欢迎大牛指点(运行通过)
    写了一份统计网站(ASP.NET)日访问量的源码(保存至数据库,部分性能待优化),运行通过。
  • 原文地址:https://www.cnblogs.com/tccbj/p/10402072.html
Copyright © 2011-2022 走看看