一、技术总结

  1. 是贪心算法的题目,题目主要考虑的问题有几个,是否会在第一个加油站的最近距离大于0,如果是这样那么直接输出答案,因为初始油箱没有汽油;

    第二个是如何选定加油站,如果在可到达距离范围类,我们优先考虑比当前加油站价格更低的,然后如果有,就直接到达这里,如果没有那也要选出这里面价格最低的那个加油站,

    然后在当前加油站,加满油箱。这样可以更加的省钱,那么油箱会多出油行驶距离(leftdis = Cmax*Davg -(minPriceDis - nowdis))。
  2. 可以开始判断最后结局了,如果到达最后一个加油站,并且加上最大行驶距离还是没有到达,那么便输出答案,无法到达。
  3. 这里可以提供一个巧妙的方法,就是在把当前目的地距离,当成一个加油站,然后判断条件是while(nowdis < D),这样就无需纠结目的地如果在加油站中间该如何计算距离的尴尬局面

    并且不好想通。
  4. for循环的时候,注意几个点,也就是小于等于,i<=N && sta[i]<=maxdis

二、参考代码

#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int inf = 999999;
struct station{
double price, dis;
};
bool cmp1(station a, station b){
return a.dis < b.dis;
}
int main(){
double Cmax, D, Davg;
int N;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
vector<station> sta(N+1);
sta[0] = {0.0, D};
for(int i = 1; i <= N; i++){
scanf("%lf%lf", &sta[i].price, &sta[i].dis);
}
sort(sta.begin(), sta.end(), cmp1);
double nowdis = 0.0, maxdis = 0.0, nowPrice = 0.0, totalPrice = 0.0, leftdis = 0.0;
if(sta[0].dis != 0){
printf("The maximum travel distance = 0.00");
return 0;
}else{
nowPrice = sta[0].price;
}
while(nowdis < D){
maxdis = nowdis + Cmax*Davg;
double minPrice = inf, minPriceDis = 0;
int flag = 0;
for(int i = 1; i <= N && sta[i].dis <= maxdis; i++){
if(sta[i].dis <= nowdis) continue;
if(sta[i].price < nowPrice){
totalPrice += (sta[i].dis - nowdis - leftdis) * nowPrice / Davg;
leftdis = 0.0;
nowPrice = sta[i].price;
nowdis = sta[i].dis;
flag = 1;
break;
}
if(sta[i].price < minPrice){
minPrice = sta[i].price;
minPriceDis = sta[i].dis;
}
}
if(flag == 0 && minPrice != inf){
totalPrice += (Cmax - leftdis/Davg) * nowPrice;
leftdis = maxdis - minPriceDis;//(Cmax*Davg - (minPriceDis - nowdis));
nowPrice = minPrice;
nowdis = minPriceDis;
}
if(flag == 0 && minPrice == inf){
//nowdis += Cmax * Davg;
printf("The maximum travel distance = %.2f", maxdis);
return 0;
}
}
printf("%.2f", totalPrice);
return 0;
}

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

  1. 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 ...

  2. 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 ...

  3. 【贪心】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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

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

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

  10. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

随机推荐

  1. 第八周论文学习03 An Efficient Tree-based Power Saving Scheme for Wireless Sensor Networks with Mobile Sink

    来源:IEEE Sensors Journal Year: 2016, Volume: 16, Issue: 20 Pages: 7545 - 7557, DOI: 10.1109/JSEN.2016 ...

  2. VS Code 提示‘未找到Git。请安装Git,或在“git.path”设置中配置’

    一.情况说明 1.描述 从Git上克隆出代码,用vscode打开项目提示“未找到Git.请安装Git,或在“git.path”设置中配置” 2.截图 二.报错原因 .没有安装Git .没有设置Git路 ...

  3. NXP官方ddr_stress_tester工具使用

    1.前言 NXP官方提供了一个DDR初始化工具,名称为ddr_stress_tester,该工具具有以下特点: 该工具能通过USB OTG接口与目标板进行连接,通过USB OTG接口完成DDR的初始化 ...

  4. Map映射如何使用迭代器?

    迭代器只针对集合类型的数据,因此map类型的必须先转换成集合类型才能使用迭代器去获取元素. 1.在map中虽然不能直接实例化迭代器,但map集合提供了keySet()方法和value()方法,可以通过 ...

  5. HTML+css基础 css的几种形式

    1.行间样式:将style写在标签内的充当标签标签属性 2.行内样式

  6. redis 阻塞原因

    1.内因: A.api或数据结构使用不合理: 如:对一个包含上万元素的hash结构执行hgetall操作,数据量大且命令复杂度O(n),必然阻塞 B.慢查询:前面有介绍 C.大对象: 执行./redi ...

  7. 如何在 C# 中自定义 Comparer,以实现按中文拼音(a-z)来排序

    1. 为何要自定义 Comparer a. 先看如下代码 class Program { public static void Main(string[] args) { List<string ...

  8. java基于NIO的分散读取文件,然后统一聚合后写入文件

    分散读取:对于一个文件,可以分散的读取数据,可以快速的读取,好比多个线程在分段同时读取: 聚合写入:为了提高效率,一般读取到的数据都是分散的,要快速写入,就需要把分散的数据聚集在一起,然后一块写入到文 ...

  9. vue 获取视频时长

    参考资料:js获取上传音视频文件的时长 直接通过element-ui自带的上传组件结合js即可,代码如下: HTML: <el-upload class="upload-demo&qu ...

  10. java都是值传递,没有引用传递

    博主这几天在复习 javaSE 部分的内容时,遇到了关于参数传值的问题,但是始终不知道原因,上网上一查才知道钻牛角尖了,把C语言的参数传值转移到java中了. 相信很多在学习java之前,有接触过C/ ...