一、技术总结

  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. Kubernetes生产架构浅谈

    注意 本文,只是笔者针对Kubernetes生产环境运行的一些关于架构设计介绍. 介绍 基于 Kubernetes 系统构建的统一开发运维管控平台.在 Kubernetes 基础上,围绕 微服务系统的 ...

  2. 多维度对比5款主流分布式MQ消息队列,妈妈再也不担心我的技术选型了

    1.引言 对于即时通讯网来说,所有的技术文章和资料都在围绕即时通讯这个技术方向进行整理和分享,这一次也不例外.对于即时通讯系统(包括IM.消息推送系统等)来说,MQ消息中件间是非常常见的基础软件,但市 ...

  3. 将服务器时间类型改为UTC(0000)

    方法一: # timedatectl  set-timezone UTC #  timedatectl set-time "YYYY-MM-DD HH:MM:SS" #  time ...

  4. Elastic Stack核心产品介绍-Elasticsearch、Logstash和Kibana

    Elastic Stack 是一系列开源产品的合集,包括 Elasticsearch.Kibana.Logstash 以及 Beats 等等,能够安全可靠地获取任何来源.任何格式的数据,并且能够实时地 ...

  5. 用Python完成毫秒级抢单,助你秒杀淘宝大单

    目录: 引言 环境 需求分析&前期准备 淘宝购物流程回顾 秒杀的实现 代码梳理 总结 0 引言 年中购物618大狂欢开始了,各大电商又开始了大力度的折扣促销,我们的小胖又给大家谋了一波福利,淘 ...

  6. 一款常用的截图工具(能够截gif动图)

    这款工具用来截程序的演示GIF图片,灰常方便. 直接上Github地址: https://github.com/NickeManarin/ScreenToGif

  7. Python【day 11】迭代器

    迭代器-用 1.迭代器的概念 1.可迭代对象-iterable str.list.tuple.dict.set.open().range() 2.可迭代对象的概念: 其数据类型的执行方法中含有__it ...

  8. 2019 梆梆安全java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.梆梆安全等公司offer,岗位是Java后端开发,因为发展原因最终选择去了梆梆安全,入职一年时间了,也成为了面 ...

  9. 解决关于 npm build --prod ,出现 ERROR in budgets, maximum exceeded for initial. Budget 5 MB was exceeded by 750 kB的问题

    问题: 执行命令 :npm build --pord,出现以下错误: WARNING :. Ignoring. WARNING MB was exceeded by 3.73 MB. ERROR MB ...

  10. 高性能TcpServer(C#) - 5.客户端管理

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...