A1033 To Fill or Not to Fill (25 分)
一、技术总结
- 是贪心算法的题目,题目主要考虑的问题有几个,是否会在第一个加油站的最近距离大于0,如果是这样那么直接输出答案,因为初始油箱没有汽油;
第二个是如何选定加油站,如果在可到达距离范围类,我们优先考虑比当前加油站价格更低的,然后如果有,就直接到达这里,如果没有那也要选出这里面价格最低的那个加油站,
然后在当前加油站,加满油箱。这样可以更加的省钱,那么油箱会多出油行驶距离(leftdis = Cmax*Davg -(minPriceDis - nowdis))。 - 可以开始判断最后结局了,如果到达最后一个加油站,并且加上最大行驶距离还是没有到达,那么便输出答案,无法到达。
- 这里可以提供一个巧妙的方法,就是在把当前目的地距离,当成一个加油站,然后判断条件是while(nowdis < D),这样就无需纠结目的地如果在加油站中间该如何计算距离的尴尬局面
并且不好想通。 - 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 分)的更多相关文章
- 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 ...
- 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 ...
- 【贪心】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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
随机推荐
- python-4-格式化输出
前言 有些小伙伴在打印中乱码或者编码不对,在这里讲格式化输出前,先讲下编码.我们都知道目前主流使用就是utf-8编码. 一.编码简介 编码用来让计算机识别,当然我们都知道计算机只能识别01010101 ...
- .NET Core 学习笔记
1. System.Composition – Using Import Attributes 截图: 2. System.Composition – Using Reflection 截图: 以后会 ...
- WPF 3D相机基本坐标简介
基本概念 WPF中3D空间基本坐标系是右手坐标系. WPF中3D空间的原点是(0,0,0) Position: 这个参数用来表示相机在空间内的坐标.参数是(X,Y,Z).当修改相机的这个参数时,这个坐 ...
- 【LOJ#3145】[APIO2019]桥梁(分块,并查集)
[LOJ#3145][APIO2019]桥梁(分块,并查集) 题面 LOJ 题解 因为某个\(\text{subtask}\)没判\(n=1\)的情况导致我自闭了很久的题目... 如果没有修改操作,可 ...
- IIS_CVE-2017-7269 IIS6.0远程代码执行漏洞复现
CVE-2017-7269 IIS6.0远程代码执行漏洞复现 一.漏洞描述 IIS 6.0默认不开启WebDAV,一旦开启了WebDAV,安装了IIS6.0的服务器将可能受到该漏洞的威胁. 二.影响版 ...
- Elasticsearch(ES) 创建索引
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- FreeMarker实现网页静态化
1.FreeMarker实现网页静态化. FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servl ...
- Laravel 运行php artisan serve命令时提示No application encryption key has been specified
创建了新的laravel项目后, 运行提示:No application encryption key has been specified 解决方法: 这个是由于没有配置好 APP_KEY 在终端上 ...
- SAP S4HANA 账户组的配置里'Int.Std.Grping'选项没勾选导致ABAP程序报错
SAP S4HANA 账户组的配置里'Int.Std.Grping'选项没勾选导致ABAP程序报错 BP,试图创建一个新的vendor code, 角色是ZGM001, Grouping是G001, ...
- block注意事项
1.block的声明和注意事项 #import "ZYViewController.h" @interface ZYViewController () @end /*用typede ...