转自:https://www.cnblogs.com/XBWer/p/3866486.html

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

题目解析:题目先输入四个数值,油箱大小,目的地离出发点的距离,每升油能跑多远,总共几个加油站;后输入每个加油站每升油的价钱和离起始点的距离。优先输出能够到达目的地的最便宜的价钱,如果是在不能到达,输出最远能到的距离。
分析题目,1.如果到达一个加油站,前方不需要加满的前提下有更便宜的加油站,当然是加到刚刚好够到下一个加油站的油就足够了;2.如果前方加油站在当前加满油的可达范围内都更贵,那肯定是加满油然后到范围内最便宜的那个;3.如果可达范围内没有加油站了,就全力向终点奔去(当然只要能到终点的油就行了),或者停到路上,这就是最远能到的距离 代码:
/*
分析:
很明显,这道题的思路是贪心,也就是要用尽可能少的钱,要去尽可能远的地方。
那么,像这种贪心的题目,怎么去思考呢?
首先,今天听到戴龙翱(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 ;
}
int curstnum=; //当前所处的油站编号,即当前的位置
double curgas=; //当前的油量
double curcost=; //当前的花费
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 ;
}

To Fill or Not to Fill (贪心)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. CENTOS6.6上搭建单实例ORACLE12C

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 自己在centos6.6上搭建的单实例oracle12c 由 ...

  2. Redis基础及入门

    一. 什么是 Redis            Redis 是一个可基于内存,有着完备的持久化机制并以 Key-Value 形式存储的非关系型数据库.也称为数据结构服务器.    二. Redis 的 ...

  3. C# HelpPage 接口文档配置

    1.打开项目路径如下的类文件: 1.1.找类方法 Register 下的 config.SetDocumentationProvider 并取消注释,修改 ~/App_Data/XmlDocument ...

  4. hdu 1207 四柱汉诺塔

    递推,汉诺塔I的变形. 这题真心没想到正确解法,越想越迷糊.这题看了别人题解过得,以后还是自己多想想,脚步太快并非好事. 贴上分析:   分析:设F[n]为所求的最小步数,显然,当n=1时,F[n]= ...

  5. HDU - 1172

    思路:假设答案是x,那么x必定满足所有语句给定的条件.例如3585和4815就有2个相同的数,1和相同的位. 只要这个数满足所有条件,那么就是一个可能的答案,当答案数量超过1个时,就是"No ...

  6. KV型内存数据库Redis

    Redis是开源的高性能内存Key-Value数据库, 可以提供事务和持久化支持, 并提供了TTL(time to life)服务. Redis采用单线程数据操作+非阻塞IO的模型,非阻塞IO提供了较 ...

  7. SpringBoot,Security4, redis共享session,分布式SESSION并发控制,同账号只能登录一次

    由于集成了spring session ,redis 共享session,导致SpringSecurity单节点的session并发控制失效, springSession 号称 无缝整合httpses ...

  8. Hive:子查询

    Hive只支持在FROM子句中使用子查询,子查询必须有名字,并且列必须唯一:SELECT ... FROM(subquery) name ...

  9. configure: error: xml2-config not found. Please check your libxml2 installation

    安装php时的报错 checking libxml2 install dir... nochecking for xml2-config path... configure: error: xml2- ...

  10. dojo表格分页之各个参数代表的意义(一)

    下面是dojo表格分页参数代表的意义 //每页可以显示10/15/20/25/30条记录 (1)pageSizes: [10, 15, 20, 25,30], //每页显示的记录从多少到多少,共多少条 ...