题目

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;
}

最新文章

  1. parawork平台介绍
  2. 记录ASP.NET Web API 服务接口响应时间
  3. 【Framework】HTTP运行期与页面执行模型
  4. DB Cache
  5. Android开发心得(转)
  6. QQ空间的“神奇”图片
  7. oracle数据表误删恢复
  8. rhel-server-6.2-i386安装gcc、g++步骤
  9. SuperSpider——打造功能强大的爬虫利器
  10. linux之SQL语句简明教程---主键,外来键
  11. 过河(bfs)
  12. jquery 上下滑动效果
  13. jsp Ajax请求(返回json数据类型)
  14. 201521044091 《Java程序设计》第13周学习总结
  15. LINUX 笔记-文件名的匹配
  16. Python中的SQLAlchemy
  17. Oracle学习笔记三
  18. (hdu 6030) Happy Necklace 找规律+矩阵快速幂
  19. .net 问题
  20. 直接添加viewController中的view时的注意事项

热门文章

  1. VUE3.x 前瞻
  2. hive表字段注释显示乱码问题
  3. window下mysql-proxy简单使用
  4. Python基础+爬虫基础
  5. php语言注意点
  6. spring+springMVC+mybatis , 项目启动遇坑
  7. Swift—UITextField的基本用法
  8. UVA 11235 RMQ算法
  9. docker搭建redis主从集群和sentinel哨兵集群,springboot客户端连接
  10. web嵌入到原生的app里需要注意的事项