【贪心】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 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. Different gas station may give different 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

/*
分析:
很明显,这道题的思路是贪心,也就是要用尽可能少的钱,要去尽可能远的地方。
那么,像这种贪心的题目,怎么去思考呢?
首先,今天听到戴龙翱(ZJU大牛)提到的一点,对于贪心的题目,测试样例,必须自己去体会一遍,这样,很有可能会给我们带来不少启发。
那么,我们就一起来过一遍测试样例吧: 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 先画个图再看
1:起点,肯定是要加油的,不然跑都跑不起来,那么,问题来了——加多少?
让我们先来观察一下,油箱加满之后,最远能跑600;也就是说,如果我现在在起点把油箱加满的话,[0,600]都是我可以达到的路程范围;
好了,那么,我们只需要看这个范围内的如何做出贪心策略;
起点处的油价是7.1,如果之后遇到的第一个加油站A油价小于7.1,那么,在A之后不管是什么情况,我们都应该加油(至于要加多少,还不能确定),
因为至少在当前状态下,这样做是最“贪婪”的。
2:通过1的分析,我们选择了加油站B。而且值得强调的是,我们在起点A加的油跑到B时是正好用完的状态。
这时,我们站在了B点,B点最远能到750(150+600),我们又如何根据贪心算法来做出贪婪的决策呢?
B处的油价是7,显然,750之前的加油站有很多,油价有比7小的,比7大的,也有等于7的。那么,贪婪的你,一定不会傻到去选择一个油价贵的(如C、E)
因为我完全可以到达比7小的加油站再加油,为什么要去比7大的加油站加油呢?
so,我们选择了D(油价6.85),而且,D的油价比当前的便宜,所以我们加油只要够从B——>D就好,加多了就不符合我贪婪的本性了!
3:到了D之后,可以说是比较开心的,因为在他[300,300+600]的范围内这价是最便宜的,此时不加更待何时!?因为是最便宜的,所以,为了贪,必须加满!
加满了之后,最远可以到900(300+600),那么,在900之前,我们会遇到E、F,且F油价较E要便宜一些,因此,为了到达目的地,我们不得不到F加油。
4:和之前的情况有所不同的是,这次,我们到目的地的时候,还是有油剩余的(600-300<600),而且剩余的油够跑300(即可以跑到900)。
那么,我们在F加多少的油呢?
站在F的位置,我们开始思考。距离400有一个加油站G,可是油价要7.3,坑爹呢!这么贵!
可是,就算F加满了,我们也只能跑到1200(600+600),所以,没办法,为了到达目的地,我们不得不到G加,但是,这里要注意,因为G比F的油价要贵,
所以,为了贪,我们会在F把油加满,(在能够达到目的地的前提下,尽可能在贵的地方少加点油,在便宜的地方多加点油——贪);
5:到了G之后,计算了此时邮箱还剩下的油狗刨200,也就是说,我们在贵的的地方G只需要加50(1250-1000-200),能到H即可,因为H的油价是最便宜(没有之一),
在[1000,1000+600]的范围内,是最便宜的,so,就这样走到了H
6:走到了H之后,就不用多想了,H之后也没有加油站了,而且加满能够到目的地I的油量就够了。 经过了以上分析之后,要开始对以上的各个情况进行抽象,即把遇到的情况分类(要包括所有的情况),并且,基于贪心的思想去考虑不同的情况下,做出何种决策
处在当前加油站(起点加油站)的情况下
情况1:600米范围内,有目的地——计算恰好能到目的地的油量 【6】
情况2:600米范围内没有加油站,无论油价多贵——加满——能跑多远算多远
情况3:600米范围内有加油站:
a:有比当前加油站的油价更便宜的加油站——加到恰好能到那个最近的油站的油量 【1】【2】【5】
(注:1、如果有多个便宜的,还是要先选取最近的那个,而不是最便宜的那个;2、可能还有油剩余)
b:没有比当前加油站的油价更便宜的加油站——加满,然后在600范围内找到最便宜的加油站加油 【3】【4】 再来看第二组数据:
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600 Sample Output 2:
The maximum travel distance = 1200.00 分析过程:
1:600的范围内(包括600),有加油站,而且比当前的油价要便宜,因此,属于情况3—a,故,我们加到恰好能到,这里比较特殊的是,只有加满才恰好能到,
注意,这里不能归为情况2,因为情况2的结果对应着一定无法到达目的地,所以,当前的状态还无法判断出能不能到达目的地;
2:600范围内,没有加油站,这里属于情况2,能跑多远跑多远,因为已经无法到达目的地了,只能尽可能地跑更远 经过以上的分析,就可以开始尝试地写代码了
特殊的情况优化:
1:起点没有加油站
2:起点即终点 主要的几个关键点,或者说是行驶的过程中需要记录些什么信息:
1:到达当前加油站的油量——因为,你要计算还需要加多少油,所以,总共需要的油量—现有的油量=在当前加油站要加的油量
*/
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std; typedef struct
{
double pos;
double price;
}gasstation;
gasstation gasst[]; bool cmp(gasstation a,gasstation b)
{
if(a.pos<b.pos)
return true;
return false;
} int main()
{
double Cmax,D,Davg;
int N;
scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
int i;
for(i=;i<N;i++)
scanf("%lf%lf",&gasst[i].price,&gasst[i].pos);
sort(gasst,gasst+N,cmp);
if(D==)
{
printf("0.00\n");
return ;
}
if(gasst[].pos!=)
{
printf("The maximum travel distance = 0.00\n");
return ;
}
113 int curstnum=0; //当前所处的油站编号,即当前的位置
114 double curgas=0; //当前的油量
115 double curcost=0; //当前的花费
bool flag=false; //是否达到目的
double maxrundis=Cmax*Davg; //邮箱加满最远能行驶的距离
while(!flag)
{
bool tag=false; //最大距离内是否有加油站
bool ifcheaper=false; //是否有便宜的
double cheapestprice=; //找出最便宜的
int cheapestnum; //没有更便宜的情况下,找出最便宜的
for(i=curstnum+;i<N;i++)
{
if((gasst[i].pos-gasst[curstnum].pos)<=maxrundis) //范围内
{
tag=true; //有加油站
if(gasst[i].price<gasst[curstnum].price) //情况3-a
{ //且有更便宜的
ifcheaper=true;
double dist=gasst[i].pos-gasst[curstnum].pos;
double needgas=dist/Davg-curgas;
curgas=;
curcost+=(needgas*gasst[curstnum].price);
curstnum=i;
break;
}
if(gasst[i].price<cheapestprice)
{
cheapestprice=gasst[i].price;
cheapestnum=i;
}
}
else
break;
}
if(!ifcheaper&&(maxrundis>=(D-gasst[curstnum].pos))) //说明已经可以到达目的地了,情况1
{
double dist=D-gasst[curstnum].pos;
double needgas=dist/Davg-curgas;
curcost+=needgas*gasst[curstnum].price;
printf("%.2lf\n",curcost);
return ;
}
if(tag&&!ifcheaper) //情况3-b
{
double needgas=Cmax-curgas;
curcost+=(needgas*gasst[curstnum].price);
double dist=gasst[cheapestnum].pos-gasst[curstnum].pos;
curgas=Cmax-dist/Davg;
curstnum=cheapestnum;
}
else if(!tag) //情况2
{
printf("The maximum travel distance = %.2lf\n",gasst[curstnum].pos+maxrundis);
return ;
}
}
return ;
}
【贪心】PAT 1033. To Fill or Not to Fill (25)的更多相关文章
- 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甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- PAT甲级——1033 To Fill or Not to Fill
1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...
- 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 ...
- 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_A1033#To Fill or Not to Fill
Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
随机推荐
- XSD 数据类型
字符串数据类型(String Data Type) 字符串数据类型可包含字符.换行.回车以及制表符. 下面是一个关于某个 scheme 中字符串声明的例子: <xs:element name=& ...
- Storm概念、原理详解及其应用(一)BaseStorm
本文借鉴官文,添加了一些解释和看法,其中有些理解,写的比较粗糙,有问题的地方希望大家指出.写这篇文章,是想把一些官文和资料中基础.重点拿出来,能总结出便于大家理解的话语.与大多数“wordcount” ...
- linux下时间同步的两种方法分享
方法1:与一个已知的时间服务器同步 复制代码 代码如下: ntpdate time.nist.gov 其中 time.nist.gov 是一个时间服务器. 删除本地时间并设置时区为上海 复制代码 代码 ...
- FastFel解析一个公式的步骤
FastFel 查看源码后,理解的运算步骤: 1) 创建一个 FelEngine,FelEngine fel = new FelEngineIml(); 2) 将表达式 exp 解析成为一个节点树 F ...
- 面向切面编程(AOP)简介
在软件中,有些行为对于大多数应用都是通用的.日志,安全和事务管理几乎是所有软件都需要的.他们是否可以主动的参与呢,如果让应用程序只关注与自己所针对的业务领域问题,而其他的问题有其他应用对象来处理.是否 ...
- 使用spring initialization创建SpringBoot项目
https://blog.csdn.net/liutong123987/article/details/79385513 有很多方法可以快速创建Springboot项目,可以通过idea的spring ...
- vs2010中 js的intellisense(智能提示) 和 Snippets(代码块)
关于智能提示vs2008和vs2010都是很强大了!但是很多我们知道但是也没有怎么合理应用,真是可惜了啊,如果合理用起来那么会提高我们的开发效率和减少出错几率哦,如果没有智能提示,我想我会疯的. 因为 ...
- level1 -- unit 3 - 频率副词
频率副词 never sometimes usually always never 从不 sometimes 有时 usually 通常 always 总是 频率排名(从最不到最经常): never ...
- php解析出带层级关系的mpp文件
本来要使用DHX gantt插件自带的API做导入,可是做完后,又发现不稳定,不能访问了 可能是屏蔽掉了 所以又想起可以使用javaBridge,借用java的MPXJ php解析mpp的 上一篇介绍 ...
- VC++6.0远程调试(亲试成功)
0 前言 VS2008及以上远程调试上篇已经讲过,这里再讲下VC++6.0开发环境下的远程调试能力,仅需下面4步即可,更方便的就接着后面的5-6步. 因为目标程序需要在有采集卡等相关硬件支持下的工控机 ...