【PAT 1033】【codeup 2031】To Fill or Not to Fill

【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟 1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan With highways available, drivi 阅读详情

题目描述

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.

输入

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.

输出

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.

样例输入

59 525 19 2
3.00 314
3.00 0

样例输出

82.89

提示

该题目所要解决的问题是:给定若干加油站信息,问能否驾驶汽车行驶一定的距离。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则最远能够行驶多长距离。

拿到这一题,首先判断汽车是否能够行驶到终点。什么情况下汽车无法行驶到终点呢?两种情况:起点根本就没有加油站,汽车无法启动;或者中途两个加油站之间的距离大于加满油后汽车能够行驶的最大距离。前者汽车行驶的最大距离为0.00,而后者最大距离为当前加油站的距离加上在这个加油站加满油后能够行驶的最大距离。在这里,需要将加油站按到杭州的距离从小到大排序。

接下来在能够行驶到终点的情况下计算最小花费。我们首先从路程来考虑,如果在路上,我们能够使用最便宜的汽油,当然就在那个加油站加油了。所以从起点开始遍历每个加油站。假设遍历到了第i个加油站,我们现在来判断在加油站i应该加多少油。设当前汽车离杭州的距离为curLen,当前加油站离杭州的距离为nodes[i].dis,加满油以后汽车能够行驶的最大距离为(dis=cmax*len)。这样就有node[i].dis <= curLen <= nodes[i].dis+dis,否则的话第i个加油站的油是不起作用的。于是在第i个加油站的作用范围内寻找有没有更为便宜的加油站,如果有,则下次使用这个加油站的油(j),这次汽车应该行驶到这个加油站,即touch=nodes[j].dis。如果没有找到更为便宜的加油站则可以在第i个加油站加满油,即touch=nodes[i].dis+dis。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)。

===========================================================================================

我觉得我已经把题目吃的很透了,但却还是过不了,(网上给的样例我全都过了),显示答案错误45%,我的思路是先排除无法到达终点的情况,再去探究可以到终点的情况。接着就是遍历加油站,先考虑是否有车辆范围内更低的油价,再考虑终点是否在车辆范围内,最后考虑车中剩余的油量,下面是代码

#include<stdio.h>
#include<algorithm>
using namespace std;

typedef  struct{
	double unit_pric;
	double gas_dist;
}Gaes;

int cmp(Gaes a,Gaes b)
{
	return a.gas_dist<b.gas_dist;
}

int main()
{
	Gaes station[505];
	double cap=0,dist=0,avg=0;
	int n=0;
	scanf("%lf%lf%lf%d",&cap,&dist,&avg,&n);
	double max=cap*avg;
	//printf("%lf %lf %lf %d\n",max,cap,avg,n);


	int i=0;
	for(;i<n;i++)
	{
		scanf("%lf%lf",&station[i].unit_pric,&station[i].gas_dist);
	}

	sort(station,station+n,cmp);

	int flag=1;
	if(station[0].gas_dist!=0)//如果起点没加油站则到不了终点
	{
		flag=0;
		printf("The maximum travel distance = %.2lf\n",0);
		return 0;
	}

	

	for(i=1;i<n;i++)//有相临两点之间的距离超过车的行程则到不了终点
	{
		if(station[i].gas_dist-station[i-1].gas_dist>max)
		{
			flag=0;
			break;
		}
	}
	if(i=n)
	{
		if(dist-station[i-1].gas_dist>max)
			flag=0;
	}
	if(!flag)
	{
		printf("The maximum travel distance = %.2lf\n",station[i-1].gas_dist+max);
		return 0;
	}
//	printf("可达到终点\n");
	//下面是不会中途没油的情况
	//double curLen=0;//当前离杭州的距离
	//double curgas=0;//当前汽车的汽油量
	double restlen=0;//汽车的当前的汽油可以行进的距离
	double touch=0;//汽车可以接触的范围,离当前的距离算
	double totalM=0;//花费的钱
	for(i=0;i<n;i++)
	{
		//if(touch)
		//	touch-=station[i].gas_dist-station[i-1].gas_dist;
		//if(touch)
		//	continue;
		int flag=0;
		touch=0;
		
		//double min=1000005;

		if(i)
			restlen-=station[i].gas_dist-station[i-1].gas_dist;//剩余距离减去行驶的距离
		//当从加油站可以触及到目的地时
		/*if(dist-station[j].gas_dist<=max)
		{
			if(restlen>dist-station[j].gas_dist)
				break;
			flag=1;
		}*/
		//printf("寻找低价\n");
		int j=i;
		for(;j<n&&station[j].gas_dist-station[i].gas_dist<=max;j++)
		{
			
			if(station[j].unit_pric<station[i].unit_pric)
				//如果在汽车可触及的范围内有比当前的油价低时
			{
				touch=station[j].gas_dist-station[i].gas_dist;
				flag=1;//有找到才退出
				break;//一但有就可以退出循环
			}
			else 
			{
				//if(station[j].unit_pric<min)
				//	min=station[j].unit_pric;
				
				touch=max;//如没有比当前小的就加满油
				//printf("touch=%lf max=%lf\n",touch,max);
			}
		}
		if(!flag&&dist-station[i].gas_dist<=max)//当没有比当前油站油价小时,且要到目的地了
		{	
			
			touch=dist-station[i].gas_dist;
			//printf("可直接到目的地 touch=%lf restlen=%lf\n",touch,restlen);
			//printf("计算价钱\n");
			totalM+=(touch-restlen)/avg*station[i].unit_pric;
			break;
		}
		
		if(touch<=restlen)//如果预计到的距离小于剩余距离
		{
			//printf("油还够 touch=%lf\n",touch);
			continue;
		}
		else//否则就要加油
		{
			//printf("计算价钱\n");
			totalM+=(touch-restlen)/avg*station[i].unit_pric;
			restlen=touch;
		}
	}
	printf("%.2lf\n",totalM);
}

不知道有没有大神帮助看下。

贪心算法:PTA A1033 To Fill or Not to Fill 的思考逻辑 核心逻辑 之所以能够贪心的前提,是不同的加油站有不同的价格,所以想要实现整体最优(最便宜的总价格),那么就不能只看到眼下的,一次性加满,不够再补加。(这样就太狂野粗放了)。 所以,要实现贪心,首先就是要往后多考虑一步。 因为题目假设的初始条件是初始油箱未空,所以如果不存在距离为0的加油站,直接打印结束。 如果有距离为0,那就可以考虑下一个加油站了(前提是已经按照距离从小到大排序。) 在考虑下一... 阅读详情

相关推荐

To Fill or Not to Fill

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.

Codeup_2031:问题 C: To Fill or Not to Fill

问题 C: To Fill or Not to Fill 时间限制 : 1.000 sec 内存限制 : 32 MB 题目描述 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. Dif

qq_45770655的博客 266

医疗信创国产化替代目前面临哪些难题?如何利用国产虚拟化技术促进医院信创建设

业务系统众多,存在大量信创改造模糊地带,既有老系统,也有新系统,故障点多,管理难度高,IT人力资源不足,其对硬件的支撑能力也提出了越来越高的要求,由此造成硬件成本、管理成本以及能耗成本都非常高。

云宏winhong的博客 2659

问题 C: To Fill or Not to Fill

问题 C: To Fill or Not to Fill 时间限制:1 Sec内存限制:32 MB 提交:598解决:131 [提交][状态][讨论版][命题人:外部导入] 题目描述 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan...

ActionBeam的博客 435

运放+三极管构建LED恒流驱动电路:原理、设计与调试全解析

在电子工程领域,恒流源是一种能够提供稳定输出电流的电路,其核心原理基于负反馈控制机制。通过将输出电流采样转化为电压信号,并与一个稳定的参考电压进行比较,系统自动调节功率器件的导通状态,从而抵消输入电压波动或负载变化带来的影响,实现电流恒定。这种技术对于驱动LED、激光二极管等电流敏感型器件具有重要价值,能确保亮度、色温的一致性,并延长器件寿命。本文以经典的运放结合三极管方案为例,深入剖析其如何实现精准的电流“钳位”。该方案利用通用运算放大器作为比较器,配合三极管构成调整管,通过采样电阻实时监测电流,形成了一

weixin_42525800的博客 496

真双口RAM---Quartus与Vivado开发环境区别对比

2、写和写冲突:表示两个端口写使能同时有效且写地址相同,此时需要关断一个写,把两个写端口都需要更新的值处理到一个写端口上面,任何的DP RAM 都不支持写和写冲突。以上述直方图统计使用为例,博主在仿真过程中发现,vivado真双口RAM--B端口读写延迟2个clk;1、读和写冲突:如果读和写同时有效,且读和写是同一个地址时,发生RAM读写冲突,此时会把最新的写数据直接赋给读数据,称为写穿通到读。

weixin_54470733的博客 3582

Codeup小白掉坑经验总结之 To Fill or Not to Fill id=2031

Codeup小白掉坑经验总结之 To Fill or Not to Fill id=2031 题目如下 2031: To Fill or Not to Fill 时间限制: 1 Sec 内存限制: 32 MB 献花: 66 解决: 19 题目描述 With highways available, driving a car from Hangzhou to any other

漫浸天空的雨色的备忘录~ 855

PLSQL 14.0.6 下载使用教程

众所周知,PLSQL 只是一个oracle第三方开发测试工具,它依赖oracle 客户端搭配使用,但是oracle 客户端有2G,相当庞大,不建议安装。所以我们如果下载安装好PLSQL,再下载好轻量级的支持远程链接的oracle客户端,这样搭配使用可以的话就最好了,答案是可以的。 第一步: PLSQL 下载与安装 PLSQL 14.0.6 下载地址 : https://www.allroundautomations.com/registered-plsqldev/ 下载下来,除了安装路径更改一下,

qq976511508的博客 2135

《算法笔记》的一些错误

《算法笔记》的一些错误 本人研读了晴神的《算法笔记》一书,发现有些错误在勘误中没有指出来,所以在这里列举一下(当然有的可能就是印刷错误) 1、261页上面,应该是node[11111].next=22222;node[22222].next=33333; node[33333].next=-1; 2、264页中间有一段: while(p=-1){ XXX=1; //XXX改成node[i].XXX=1 count++; p=node[p]->next; } 3

wordnice2003的博客 486

手把手教你设计STM32的JTAG电路:从原理图到PCB布局(含JLINK连接详解)

本文详细介绍了STM32的JTAG电路设计全流程,从核心原理、接口规范到PCB布局布线技巧,特别包含JLINK调试器的连接与配置方法。通过信号滤波、上拉配置和防反接保护等关键设计要点,帮助开发者构建稳定可靠的JTAG调试环境,提升嵌入式开发效率。

weixin_29159441的博客 389

TwinCAT 3 实战指南:从系统配置到高级运动控制

本文详细介绍了TwinCAT 3工业自动化控制平台的实战应用,从系统配置、PLC编程到EtherCAT总线配置和高级运动控制。通过倍福(Beckhoff)的TwinCAT 3平台,工程师可以高效实现复杂的工业自动化任务,包括运动控制、PLC编程等。文章还提供了常见问题排查和性能优化技巧,帮助用户提升系统稳定性和实时性。

weixin_30831529的博客 304

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

weixin_30371469的博客 521

33、密码学中的选择密文攻击与Cramer - Shoup加密方案

本文详细介绍了密码学中选择密文攻击的背景及其对Cramer-Shoup公钥加密方案的影响。该方案在决策Diffie-Hellman假设和抗碰撞哈希函数的基础上,能够有效抵御自适应选择密文攻击,并且其安全性无需依赖随机预言机模型。文章还分析了Cramer-Shoup方案的设计原理、加密与解密过程,以及其在标准假设下的安全性证明方法。此外,通过一系列相关练习,帮助读者进一步理解密码学概念如密文不可区分性、ElGamal加密、Goldwasser-Micali方案等。最后,文章展望了密码学的未来发展趋势,包括量子

vscode6remote的博客 100

codeup 2031 PAT 1033 To Fill or Not to Fill

codeup 2031 To Fill or Not to Fill 题目描述 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...

yuxiaoyan1998的博客 212

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[codeup 2031]To Fill or Not to Fill

问题 C:2031 To Fill or Not to Fill 题目描述 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...

机器会学习的博客 558

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

A1033 To Fill or Not to Fill(贪心)

题目描述 求最小油量花费的问题 基本思想: 自己做的话的确会忽略很多细节,参考代码是引用算法笔记中的,注释写的很详细了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=510; const int INF=1000000000;...

弋墨尘的博客 227

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

RPG_Zero的博客 815

codeup|问题 C: To Fill or Not to Fill

题目描述 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 t

weixin_43340821的博客 216

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

热心市民小黎的博客 2340

Codeup/PAT To Fill or Not to Fill(贪心思想)

To Fill or Not to Fill 时间限制:1.000Sec内存限制:32 MB 题目描述 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...

Syy 251

PAT A1033:To Fill or Not to Fill之贪心求解

题目描述 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 ...

lilian的博客 219

【OJ】贪心算法:To Fill or Not to Fill——怀疑人生

题目描述 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. Differ...

Asber的博客 466

【PAT甲级】1033 To Fill or Not to Fill(贪心)

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

Y_CanFly的小小博客 431

匹配多种时间格式的正则表达式,例如用0补齐, 只含年月, 或只含时分, 而且还短

([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2]|[1-9])(-(0[1-9]|[1-2][0-9]|3[0-1]|[1-9]))?)? 匹配yyyy-MM-dd或yyyy或yyyy-MM, 日期可以0开头,也可以不是0开头 ([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)...

SeptDays的博客 2760
上一篇: C语言算法入门常见错误
下一篇: 基于java的简易邮件收发软件
吐司吐丝
博客等级 码龄10年 25粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值