题目

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. Diferent gas station may give diferent 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 (<=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

题目分析

从初始地到目的地,已知沿途高速各加油站价格,找出最省钱的加油方式

解题思路

  1. 如果一整箱油可行驶距离<当前站到下一站的距离,那么车子无法到达下一站,打印最远行驶距离(即当前站距起始点距离+一整箱油可行驶距离)
  2. 在一整箱油可到达的距离内,(当前站记为A站,后续油站中最低油价站记为B站,后续油站中小于当前站油价价格的站记为C站)

    2.1 如果当前站价格后续油站中最低价还低,加满油直到后续油站中最低价油站B(到达B站后可能油箱还有剩余)

    2.2 如果后续油站中能找到小于当前站油价的站C,加油量=刚好到达C站,在C站再加油(到达C站无剩余油)
  3. 将目的地记做"最后一个加油站",其油价设置为0(作为哨兵),若目的地为当前站一整箱油可达距离内,加油量只需要当前站到达最后目的地即可,无需多余

Code

Code 01

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
const double inf = 99999999;
struct station {
double d; // distance
double p; // price
};
bool cmp(station &s1,station &s2) {
return s1.d<s2.d;
}
int main(int argc, char * argv[]) {
double CM,D,DA;
int N;
scanf("%lf %lf %lf %d",&CM,&D,&DA,&N);
vector<station> vss(N+1);
for(int i=0; i<N; i++) {
scanf("%lf %lf", &vss[i].p, &vss[i].d);
}
vss[N].d=D,vss[N].p=0.0; //哨兵
sort(vss.begin(),vss.end(),cmp); //按照距离排序 if(vss[0].d!=0) {
//起始油箱空,并且起始位置没有加油站
printf("The maximum travel distance = 0.00"); //注意是0.00,而不是0,否则第三个测试点错误
return 0;
}
double nowd=0.0,nowp=vss[0].p,fd=0.0,ap=0.0; //ap总费用;fd到站油箱剩余油可行驶路程
double SM = CM*DA; //一箱油可行驶路程
while(nowd<D) {
int maxd=nowd+SM; // 当前邮箱加满可行驶的最远距离
double minp=inf,mind=0.0;
bool flag = false;
for(int i=0; i<=N&&vss[i].d<=maxd; i++) {
if(vss[i].d<=nowd)continue;
if(vss[i].p<nowp) {
ap+=(vss[i].d-nowd-fd)*nowp/DA;
nowd=vss[i].d;
nowp=vss[i].p;
fd=0.0; //加的油刚好到最低价的油站
flag = true;
break;
}
if(minp>vss[i].p) {
minp=vss[i].p;
mind=vss[i].d;
}
}
if(!flag&&minp!=inf) { // 之后的站没有比当前站价格更便宜,取后面站中最低价的站C
//因为当前站价格更低,装满油箱,行驶到C站,再加油
ap+=((CM-fd/DA)*nowp);
fd=SM-(mind-nowd);
nowd=mind;
nowp=minp;
}
if(!flag&&minp==inf) { // 即使油箱满油,也不足以行驶到下一站
printf("The maximum travel distance = %.2f", nowd+SM);
return 0;
}
}
printf("%.2f", ap);
return 0;
}

PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]的更多相关文章

  1. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  2. PAT Advanced 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 c ...

  3. PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)

    简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  4. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

    简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  6. PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

    树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT (Advanced Level) 1006. Sign In and Sign Out (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  9. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

随机推荐

  1. 042-PHP使用闭包函数递归无限级分类

    <?php //使用闭包函数递归无限级分类 function demo($array){ # 用于存储递归后的队列 $data = []; # 递归函数 $func = function (&a ...

  2. 020-PHP浏览目录

    <?php // 使用表格浏览目录的结构 print("<TABLE BORDER= '1'>"); // 创建表格的头 print("<TR&g ...

  3. 017-PHP数组形式读取TXT记事本内容

    <?php // 打开文件同时,输出每一行 $myFile = file("data.txt"); for ($index = 0; $index < count($m ...

  4. Bulma CSS - 模块化

    Bulma CSS框架教程 Bulma CSS – 简介 Bulma CSS – 开始 Bulma CSS – CSS类 Bulma CSS – 模块化 Bulma CSS – 响应式 Bulma框架 ...

  5. python连接 ssh

    import paramiko # private = paramiko.RSAKey.from_private_key() 秘钥 trans = paramiko.Transport((" ...

  6. AVCodecContext 结构体

    typedef struct AVCodecContext { int bit_rate; int frame_number; //扩展数据,如mov 格式中audio trak 中aac 格式中es ...

  7. Vulkan SDK 之 Depth Buffer

    深度缓冲是可选的,比如渲染一个3D的立方体的时候,就需要用到深度缓冲.Swapchain就算有多个images,此时深度缓冲区也只需要一个.vkCreateSwapchainKHR 会创建所有需要的i ...

  8. QSignalMapper is deprecated

    今天参考 qt4 的书籍,在 qt5 的平台上面,用了 QSignalMapper,结果收到警告" QSignalMapper is deprecated". 经过一番查找,找到了 ...

  9. POJ - 3279 Fliptile(反转---开关问题)

    题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 1.枚举第一行 ...

  10. NtQueryObject 获得内核对象使用计数

    一个内核对象有两个计数器:一个是句柄计数,句柄是给用户态用的:另一个是指针计数,也叫引用计数,因为核心态也常常用到内核对象,为了方便,在核心态的代码用指针直接访问对象,所以Object   Manag ...