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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤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

分析:贪心,开始没做出来,后面参考晴神宝典。

假设当前加油站编号为now,每次从当前点开始搜索最大距离范围内的下一个要前往的加油站。

分3种情况:1、找到范围内第一个比当前加油站价格低的加油站k,加恰好能到达k的油,前往k。2、如果找不到比当前加油站价格低的加油站,则找范围内最小价格的加油站k,在当前加油站加满油,前往k。3、如果在满油状态下都找不到能到达的加油站,则最远能到达的距离为当前加油站的距离加上满油状态下能前进的距离,结束算法

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-25-22.01.39
 * Description : A1033
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 ;
 struct Node{
     double price;
     double d;
 }node[maxn];
 bool cmp(Node a,Node b){
     return a.d<b.d;
 }
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n;
     double cmax,d,avg;
     cin>>cmax>>d>>avg>>n;
     ;i<n;i++){
         scanf("%lf %lf",&node[i].price,&node[i].d);
     }
     node[n].price=;
     node[n].d=d;  //终点
     sort(node,node+n,cmp);
     ].d!=){
         printf("The maximum travel distance = 0.00\n");
         ;
     }
     ; //当前加油站编号
     ,nowTank=,MAX=cmax*avg;//总花费、当前油量、最大行驶距离
     while(now < n){
         ; //最低油价的加油站编号
         double priceMin=INF;
         ;i<=n&&node[i].d-node[now].d<=MAX;i++){ //在最大行驶距离之内找到油价最低的站
             if(node[i].price<priceMin){
                 priceMin=node[i].price;
                 k=i;
                 //如果找到第一个油价低于当前油价的加油站,直接去那个加油站加油(跳出循环)
                 if(priceMin<node[now].price){
                     break;
                 }
             }
         }
         ) break; //没找到,跳出循环输出最大行驶距离
         double need=(node[k].d-node[now].d)/avg;
         if(priceMin<node[now].price){
             if(nowTank<need){
                 totalPrice+=(need-nowTank)*node[now].price;
                 nowTank=;
             }
             else{
                 nowTank-=need;
             }
         }
         else{
             totalPrice+=(cmax-nowTank)*node[now].price;
             nowTank=cmax-need;
         }
         now=k;
     }
     if(now==n){
         printf("%.2f\n",totalPrice);
     }
     else{
         printf("The maximum travel distance = %.2f\n",node[now].d+MAX);
     }
     ;
 }


1033 To Fill or Not to Fill (25 分)的更多相关文章

  1. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

  2. 【贪心】PAT 1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  3. PAT 甲级 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 oth ...

  4. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  5. pat1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  6. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

  7. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  8. PAT甲级——1033 To Fill or Not to Fill

    1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...

  9. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

随机推荐

  1. xampp for mac 本地服务器的使用

    1.下载xampp安装包 ,百度搜索下载合适版本安装包 2. 安装.下一步 ...->完成 3. 打开看到xampp启动页面. 打开你安装的路径:我这里的路径是:C:\xampp: 找见xamp ...

  2. 逆向路由器固件之敏感信息泄露 Part2

    之前的文章中详细介绍了各种解包路由器固件的工具.解包之后就获得了固件中的文件.下一步就是分析文件寻找漏洞了.这次分析的目标是Trendnet路由器,分析的漏洞是一个远程获取路由器权限的漏洞. 初步分析 ...

  3. 源代码管理:SVN源代码管理器在ASP.NET VS中的使用注意事项

    一共有三个软件 1.ASP.NET下SVN有三个是不受管理的,bin文件夹,obj文件夹,.user类型文件,位置在TortoiseSVN的Settings下面的Subversion下的[Global ...

  4. 建立自己的Servlet--成功

    1--用记事本新建一个servlet程序,文件名为HelloWorld.java,文件内容如下: import java.io.*; import javax.servlet.*; import ja ...

  5. Ubuntu 搭建Ghost1.0博客系统

    最近想使用Ghost搭建自己的博客网站,网上搜索了下大多都是1.0之前版本搭建的文章,但是Ghost1.0版本已经可用好一段时间了,所以决定根据官方文档搭建Ghost1.0版本的博客系统. 下面开始一 ...

  6. Home Kit框架简介

    重要:本文是针对开发过程中使用的API或者技术的初步文档.苹果提供该文档旨在为开发者使用该技术和苹果产品上的编程接口提供帮助.这些信息可能会发生变化,依据该文档开发的软件应该使用最新的操作系统软件和最 ...

  7. sql,去重

    distinct SELECT distinct nf FROM BSHGJJK.T_JJ_NY_QSNCJBQK order by nf desc 来自为知笔记(Wiz)

  8. TX2平台CAN总线收发功能的测试

    前言 项目实现过程中需要将获取的数据信息通过CAN总线传输到控制规划模块,本文主要介绍如何在TX2平台测试CAN总线的收发功能. TX2是英伟达旗下为嵌入式平台人工智能应用开发出的一个硬件平台,TX1 ...

  9. python绘制树枝

    python是解释型语言,下面的程序深刻的说明了这个问题. import turtle def branch(length,level): if level<=0: return turtle. ...

  10. BZOJ4310: 跳蚤 【后缀数组+二分】

    Description 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究.首先,他会把串 分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典 ...