【贪心】PAT 1033. To Fill or Not to Fill (25)

时间:2022-09-04 18:01:49

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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

【贪心】PAT 1033. To Fill or Not to Fill (25)

 /*
分析:
很明显,这道题的思路是贪心,也就是要用尽可能少的钱,要去尽可能远的地方。
那么,像这种贪心的题目,怎么去思考呢?
首先,今天听到戴龙翱(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)的更多相关文章

  1. PAT 甲级 1033 To Fill or Not to Fill &lpar;25 分&rpar;(贪心&comma;误以为动态规划&comma;忽视了油量问题)&ast;

    1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any oth ...

  2. PAT甲级1033&period; To Fill or Not to Fill

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

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

  4. PAT 1033 To Fill or Not to Fill&lbrack;dp&rsqb;

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

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

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

  7. 1033&period; To Fill or Not to Fill &lpar;25&rpar;

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

  8. PAT&lowbar;A1033&num;To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

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

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

随机推荐

  1. 【转】开放api接口签名验证

    不要急,源代码分享在最底部,先问大家一个问题,你在写开放的API接口时是如何保证数据的安全性的?先来看看有哪些安全性问题在开放的api接口中,我们通过http Post或者Get方式请求服务器的时候, ...

  2. 浅谈 Python 的 with 语句

    with 语句是在 Python 2.5 版本引入的,从 2.6 版本开始成为缺省的功能.with 语句作为 try/finally 编码范式的一种替代,用于对资源访问进行控制的场合.本章对 with ...

  3. iOS 获取设备信息,mac地址,IP地址,设备名称

    #import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...

  4. Github最流行的10&comma;000个Java项目使用的类库

    本文由 ImportNew - Andy.Song 翻译自 takipiblog.欢迎加入翻译小组.转载请见文末要求. 前言 作为Java开发人员,总是需要面临这门不断成熟.高速改进中的语言.开发人员 ...

  5. 微信小程序案例大全

    微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo: ...

  6. Intellij创建简单Springboot项目

    Intellij创建简单Springboot项目 第一步:选择创建新项目——file-new-project 第二步:选择项目类型——Spring Initializr-next 第三步:输入项目信息 ...

  7. &lbrack;android&rsqb; 新闻客户端引入SlidingMenu

    下载SlidingMenu,https://github.com/jfeinstein10/SlidingMenu 导入library 我们项目右键==>Properties==>Andr ...

  8. SQL2008无法连接到(local)&comma;该账户当前被锁定,所以Sa用户登陆失败

    1 安装小结 换了电脑,很多软件都得重装,期间报了很多问题,比如说先装vs2008再装sql server2008r2会报一个“存在2008早期版本”,通过查找,百度一系列的坑爹之路后,我还是把vs2 ...

  9. 20155209 实验三 敏捷开发与XP实践

    20155209 实验三 敏捷开发与XP实践 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 提交点一: 在IDEA中使用工具(Code->Reformate Code)把下面代码 ...

  10. 升级Xcode之后VVDocumenter-Xcode不能用的解决办法

    VVDocumenter-Xcode上一款快速添加标准注释,并可以自动生成文档的插件.有了VVDocumenter-Xcode Objective-C效果图: Swift效果图:从UUID证书从而保证 ...