zoukankan      html  css  js  c++  java
  • 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: 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​​ (≤), the distance between this station and Hangzhou, for ,. 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 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<stack>
     7 #include<algorithm>
     8 #include<string>
     9 #include<cmath>
    10 using namespace std;
    11 struct station {
    12     double price,dist;
    13 };
    14 bool compare(const station& a, const station& b)
    15 {
    16     return a.dist < b.dist;
    17 }
    18 int main()
    19 {
    20     double cmax = 0, d = 0, davg = 0;
    21     int n = 0;
    22     cin >> cmax >> d >> davg >> n;
    23     vector<station> s(n + 1);
    24     s[0] = { 0.0,d };
    25     double price,dist;
    26     for (int i = 1; i <= n; i++)
    27     {
    28         cin >> price >> dist;
    29         s[i] = { price,dist };
    30     }
    31     sort(s.begin(), s.end(), compare);
    32     double nowdist=0, maxdist=0,leftdist=0,nowprice=0, totalprice=0;
    33     if (s[0].dist != 0)
    34     {
    35         printf("The maximum travel distance = 0.00");
    36         return 0;
    37     }
    38     else
    39         nowprice = s[0].price;
    40     while (nowdist<d)
    41     {
    42         double minprice = INT_MAX, mindist = -1;
    43         maxdist = nowdist + cmax * davg;
    44         int flag = 0;
    45         for (int i = 1; i <= n&&s[i].dist<=maxdist; i++)
    46         {
    47             if (s[i].dist <=nowdist)continue;
    48             if (s[i].price < nowprice)
    49             {
    50                 totalprice += (s[i].dist - nowdist - leftdist) * nowprice / davg;
    51                 leftdist = 0;
    52                 nowdist = s[i].dist;
    53                 nowprice = s[i].price;
    54                 flag = 1;
    55                 break;
    56             }
    57             if (s[i].price < minprice)
    58             {
    59                 minprice = s[i].price;
    60                 mindist = s[i].dist;
    61             }
    62         }
    63         if (flag == 0 && minprice != INT_MAX)
    64         {
    65             totalprice += (nowprice * (cmax - leftdist / davg)); 
    66             leftdist = cmax * davg - (mindist - nowdist);
    67             nowdist = mindist;
    68             nowprice = minprice;
    69         }
    70         if (flag == 0 && minprice == INT_MAX)
    71         {
    72             nowdist += cmax * davg;
    73             printf("The maximum travel distance = %.2f", nowdist);
    74             return 0;
    75         }
    76     }
    77     printf("%.2f", totalprice);
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    XSS攻击
    逻辑题目
    PHP性能监测的工具介绍
    PHP最简单的后门,且难查,不报毒!
    php Session存储到Redis和memcached的方法
    [sourceTree]这是一个无效的源路径
    Java 解析 Excel 工具 easyexcel
    GoldenGate 将oracle数据抓取到Redis
    JAVA调用ORACLE存储过程时间类型参数没有日期
    mongoDB 分组并对分组结果筛选类似于SQL中的(group by xxx having ) 附带Java代码
  • 原文地址:https://www.cnblogs.com/57one/p/12006813.html
Copyright © 2011-2022 走看看