题目

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. centos7下使用yum安装ifconfig工具

    步骤1:搜索安装包 步骤2:使用yum安装 至此,ifconfig工具安装完毕,希望对你有帮助~

  2. jenkins 最新版 搭建

    jenkins 中文网:https://jenkins.io/zh/ 点击下载:https://jenkins.io/zh/download/ 然后选择对应的安装环境,我的CentOS 7.6: 有外 ...

  3. 转载:HTTP 请求头中的 X-Forwarded-For

    本文转自:https://www.jianshu.com/p/15f3498a7fad X-Forwarded-For和相关几个头部的理解 $remote_addr    是nginx与客户端进行TC ...

  4. ES6中字符串的新增方法梳理

    1.String.fromCodePoint(); String,fromCodePoint()方法可以认为是对String.fromCharCode()方法的扩展,这两个方法的共同点在于都是用于Un ...

  5. Ubantu学习笔记1

    重启后按e键进行编辑,在文档倒数第二行r0处修改为rw init=/bin/bash 然后F10操作,输入passwd zichua =>修改此用户名的密码,重新输入两次密码(这里密码是看不到的 ...

  6. 【Android】家庭记账本手机版开发报告三

    一.说在前面 昨天 对第一天的框架结构进行了四方面的完善 今天 对界面显示和逻辑结构进行完善 问题 无 二.界面展示完善 1.使用可回收的列表recyclerView展示账单的信息,并设置数据项为卡片 ...

  7. CCCC L3-015. 球队“食物链”(dfs+剪枝)

    题意: 某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从中找出一条包含所有球队的 ...

  8. idea修改web项目的访问路径

    转 新建好了项目发现项目只能以localhost:8080这样的访问路径访问到主页,也就是index.jsp 那么之前我用eclipse新建的项目都是localhost:8080/xxx(项目名称)来 ...

  9. storm on yarn(CDH5) 部署笔记

    按照storm on yarn(Apache hadoop)部署好之后,然后修改HADOOP_HOME,hadoopenv.sh中的JAVA_HOME,以及storm-yarn-master中pom. ...

  10. DevOps专题|玩转Kubernetes网络

    Kubernetes无疑是当前最火热的容器编排工具,网络是kubernetes中非常重要的一环, 本文主要介绍一些相应的网络原理及术语,以及kubernetes中的网络方案和对比. Kubernete ...