• 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: 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

    终于碰到了一个贪心的问题,挺有意思的。
    要从价格出发去寻找就ok
     1 #include <bits/stdc++.h>
     2 #define N 600
     3 using namespace std;
     4 double a, all, b;
     5 int n; 
     6 struct Node{    
     7     int dis;
     8     double price;
     9     friend bool operator < (const Node &a, const Node &b){
    10         return a.dis < b.dis;
    11     }
    12 }node[N];
    13 int main(){
    14     cin >> a >> all >> b >> n;
    15     for(int i = 0 ; i < n; i++){
    16         cin >> node[i].price >> node[i].dis;
    17     }
    18     node[n].dis = all,node[n].price = 0;
    19     double ans = a*b;
    20     sort(node,node+n+1);
    21     for(int i = 0; i < n; i++){
    22         if(i == 0){
    23             if(node[i].dis != 0){
    24                 printf("The maximum travel distance = 0.00
    ");
    25                 return 0;
    26             }
    27         }
    28         if(node[i].dis + ans < node[i+1].dis){
    29             printf("The maximum travel distance = %0.2lf
    ",node[i].dis + ans);
    30             return 0;
    31         }
    32     }
    33     double sum = 0.0, distans = 0;
    34     for(int i = 0 ; i < n; i++){
    35         if(distans == all)
    36             break;
    37         int j = i+1;
    38         double min_dis = min(node[i].dis + ans, all);
    39         while(j<n){
    40             if(node[j].dis - node[i].dis > ans)
    41                 break;
    42             else{
    43                 if(node[i].price > node[j].price){
    44                     min_dis = node[j].dis;
    45                     break;
    46                 }
    47             }
    48             j++;
    49         }
    50         if(min_dis <= distans)
    51             continue;
    52         sum += node[i].price*(min_dis-distans)/b;
    53         distans = min_dis;
    54     }
    55     printf("%0.2lf
    ", sum);
    56     return 0;
    57 }


  • 相关阅读:
    github入门到上传本地项目
    17-索引
    16-pymysql模块的使用
    15-可视化工具Navicat的使用
    C语言/C++知识
    Apk反编译那些事
    CTF基本常识
    WebView net::ERR_CLEARTEXT_NOT_PERMITTED&&net::ERR_INTERNET_DISCONNECTED
    测试
    zookeeper-3.4.5安装&3台机器安装之后 ./zkServer.sh status 之后会显示“Error contacting service. It is probably not running.”的解决办法
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11078089.html
Copyright © 2020-2023  润新知