1033 To Fill or Not to Fill (25 point(s))

雅思听力词组总结 雅思听力词组总结(涵盖几乎所有雅思真题词组)已更新至1037条 阅读详情

1033 To Fill or Not to Fill (25 point(s))

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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then Nlines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤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 Xis 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

参考链接:

https://blog.youkuaiyun.com/xyt8023y/article/details/46425435

https://blog.youkuaiyun.com/joyceyang_999/article/details/82730818

复杂模拟题。难!

到达加油站,在当前的最大可达范围内:

 A.有其它加油站

1.如果有加油站比该站的价格低,那么加油使到达最近的站; 

2.如果有加油站但是价格更贵

a)不能到达终点,则加满,然后到可达范围内最便宜的加油站再判断(加满是因为,虽然在范围之外更低的油价,但是即使加满也无法到达,还需要中间一个加油站中转,显然这个中转加油站的油价更高,所以既然当前油价最低,能加满就加满,之后更高油价的油就可以少加点)

b)可以到达终点,在当前位置加满所需的油(或者不再加油),刚好到达终点 

B.没有其它的加油站

1.可以到达终点,在当前位置加满所需的油(或者不再加油),直接到达

2.不能到达终点,加满油后能跑多远跑多远

#include<iostream>
#include<algorithm>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
struct station{
	double price,dis;//油价,离杭州距离,到下一个油站距离 
	bool operator < (const station &other) const{
		return dis<other.dis;
	}
};
vector<station> s;
int main(void){
	double c_max,D,d_avg;int N;//油箱容量,距离杭州的距离,公里/L,油站数量
	cin>>c_max>>D>>d_avg>>N;
	station sta;
	for(int i=0;i<N;i++){
		cin>>sta.price>>sta.dis;
		s.push_back(sta);
	}
	sort(s.begin(),s.end());
	/*到达加油站,在当前的最大可达范围内:
	A.有其它加油站
	1.如果有加油站比该站的价格低,那么加油使到达最近的站; 
	2.如果有加油站但是价格更贵
	a)不能到达终点,则加满(加满是因为,无论在范围之外有多小的油价,即使加满也无法到达,还需要中间一个加油站中转,但既然当前油价最低,能加满就加满,之后油钱高的就可以少加点)
	b)可以到达终点,加油使得刚好到达终点 
	B.没有其它的加油站
	1.可以到达终点,直接到达
	2.不能到达终点,加满油后能跑多远跑多远 
	*/
	if(s[0].dis>0){
		cout<<"The maximum travel distance = 0.00"<<endl;
		return 0; 
	} 
	double sumDis,sumPrice=0;//当前距离,总价格 
	double curCap=0;//当前油量
	int cur=0;//当前所在的油站 
	double maxToGo = c_max*d_avg;//最大可达范围 
	while(cur<s.size()){ 
		bool hasStation= false; 
		double stationPrice = s[cur].price;
		double stationDis = s[cur].dis;
		int dest=-1;
		
		for(int i=cur+1;i<s.size();i++){
			if(s[i].dis<=stationDis+maxToGo){
				hasStation = true;
				if(stationPrice>s[i].price){
					dest = i;break;//记录最近的低油价站 
				}
			}
			else break;
		}
		if(hasStation == false){//当前可达范围内没有油站了 
			if(stationDis+maxToGo>=D){//如果可以到达终点 
				double restDis = D-stationDis;//剩余的距离 
				if(curCap*d_avg>=restDis){//如果不再需要额外的油
					break;
				}else{
					double needDis = restDis - curCap * d_avg;//需要加油到达的路程 
					sumPrice+=(needDis / d_avg) *stationPrice; 
					break; 
				} 
			}else{//如果不能到达终点 
				sumDis = stationDis+maxToGo;
				sumPrice += (D-curCap)*stationPrice;
				printf("The maximum travel distance = %.2lf\n",sumDis);
                return 0;
			}
		}
		else{//当前可达范围内有油站
			if(dest!=-1){//在可达范围内有比当前油站更便宜的油站 
				double gapDis = s[dest].dis - stationDis;//到最近的油站的距离 
				if(curCap*d_avg >= gapDis) curCap-= gapDis/d_avg;//当前的油量足够
				else{//需要额外的加油,加油使得能够刚好到这个站 
					sumPrice +=(gapDis-curCap*d_avg)/ d_avg*stationPrice;
					curCap = 0;//因为是刚好的,跑过去就没有油了 
				} 
				cur=dest; 
			} 
			else{//在可达范围内没有更便宜的油
				if(stationDis+maxToGo>=D){//在可达范围内可以到达终点,直接到达 
				 	double restDis = D-stationDis;//如果可以通过加油直接到达 
				 	if(curCap*d_avg < restDis){//还需要额外的油 
				 		sumPrice+=(restDis-curCap*d_avg)/d_avg*stationPrice;
				 		break;
					} 
					else break;//不需要额外的油了,直接结束 
				} 
				//在可达范围内不可以到达终点,没有更便宜的油,加满,然后到达可达范围内最便宜的下一个油站 
				int minPrice = INF;
				int minCur = -1;
				for(int i=cur+1;i<s.size();i++){
					if(s[i].dis < stationDis + maxToGo){
						if(s[i].price < minPrice){
							minPrice = s[i].price;
							minCur = i;
						} 
					} 
					else break;
				} 
				cur = minCur;// 到达可达范围内最便宜的下一个油站  
				sumPrice +=(c_max-curCap)*stationPrice;//加上达到满油的价格 
				curCap = c_max-(s[minCur].dis-stationDis)/d_avg;	//调整达到下一个油站的油量 
			}
		} 
	}
	printf("%.2lf\n",sumPrice);
	return 0; 
}

 

大模型面试题52:LLM如果在训练过程中loss值出现spike应该怎么办? 小白急救:停更→回滚→降LR→裁剪梯度,先保住训练不崩溃;进阶排查:查数据→查优化器→查硬件,找到spike的根因并解决;深层预防:预处理保守配置+训练中实时监控,从源头规避spike。先止损,再溯源,最后预防。 阅读详情

相关推荐

ArcGIS插值可视化气象数据

前言 地图底图的制作和气象数据的预处理见我上两篇博客 ArcGIS精美中国地图制作(详解) python处理日值气象数据 在前面我们已经将日值的地温数据处理成了年均地温,添加了站点经纬度坐标并导出为excel格式。 得到如下数据: 导入数据 在Arcmap中 文件-添加XY数据 将年均地温和站点位置数据导入。 注意:excel表格的格式要另存为97-2003工作簿形式,否则会有"arcgi...

江北20190411的博客 2万+

【PAT(甲级)】1033 To Fill or Not to Fill(详细的解题思路)

【PAT(甲级)】1033 To Fill or Not to Fill(详细的解题思路) 题目给出油箱容量(C),目的地距离(D),平均每升汽油可以行驶的里程数(D_avg),加油站数量(N),然后还有每个加油站距离杭州的距离和油价。要求我们给出到达目的地时,怎么样加油最省钱(一开始的车是没有油的),如果到不了目的地,那么最远能开到哪里。 1. 自定义一个结构来存储加油站信息,将加油站由近到远排序。如果最近的加油站距离杭州不是0,那么直接输出。因为车一开始是没有油的,加油站在出生点没有的话,就没办法开。

weixin_55202895的博客 2113

数据中心GPU芯片性能参数速查表(Nvidia+国产算力卡)

本文提供了一份数据中心GPU芯片性能参数速查表,涵盖Nvidia与国产算力卡两大阵营。文章详细解读了算力、显存、互连技术及功耗等关键性能指标,并对比了A100、H100、昇腾910B等主流型号,旨在帮助用户根据AI训练、推理等实际场景,做出高效的硬件选型与成本评估决策。

a0b1c2d3的专栏 836

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

小白的博客 836

VIVADO中的IP核在MATLAB中仿真

VIVADO中的IP核在MATLAB中仿真项目简述VIVADO建立FFT IP核MATLAB调用FFT IP核 项目简述 我们都会用MATLAB进行算法验证,但是经常会发现如果VIVADO中调用了IP核,那么最终会发现VIVADO与MATLAB之间的数据不可以统一的对应起来。那么,MATLAB验证的效果就会大大下降,那么可不可以想办法将VIVADO中的IP核封装成一个函数在MATLAB中调用呢?答案是可以的。这篇博客主要以FFT的IP核为例讲解封装成一个函数并且成功在MATLAB中调用。 本次实验所使用的软

zhangningning1996的博客 6992

生信实战:用bcftools玩转VCF文件处理(从压缩到注释全流程)

本文详细介绍了使用bcftools处理VCF文件的全流程实战指南。从数据准备、压缩索引、信息提取、质量统计到核心的变异过滤与功能注释,手把手教你高效管理基因组变异数据。重点阐述了如何利用bcftools进行VCF文件注释,整合外部数据库信息,为变异位点添加关键的生物学标签,是生物信息学数据分析的必备技能。

rgv23456789的博客 341

pat a1033

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

qq_41474792的博客 263

5分钟搞定阿里云千问大模型API申请:手把手教你获取API Key

本文提供了一份高效的阿里云通义千问大模型API Key获取实战指南。通过清晰的步骤,帮助开发者在五分钟内完成从账号准备、精准导航控制台到创建并验证API Key的全过程,并强调了密钥安全管理和基础调用测试的重要性,助力快速集成AI能力。

ol789012345的博客 1348

1033 To Fill or Not to Fill (25 point(s)) - C语言 PAT 甲级

设计思路: 贪心算法: * 注意车子最开始是没油的,若第一个加油站距离不为 0,即杭州没有加油站,汽车哪也去不了 * 手动添加一个在目的地的加油站,油价设为 0,在贪心策略下,汽车一定需要经过此站,以此判断是否能到达目的地,简化程序中的判断 1. 在能够到达的最大距离内寻找加油站,如果找到一个比当前油价低的,那么下一站就是这个油价低的加油站,在当前加油站加油到刚好能到达下一站即可 2. 在能够到达的最大距离内没有比当前价格更低的加油站,那就找尽可能低的加油站当做下一站,在当前加油站加满油前往;

有三只土豆 317

【WRF-Chem工具】pyVPRM 计算大气与陆地植被之间的 CO₂ 交换通量

pyVPRM 计算大气与陆地植被之间的 CO₂ 交换通量

WW、forever的博客 210

二十一、SAP-库存管理

业务逻辑、功能讲解、图文解析、章节总结

m0_72403327的博客 1810

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

人工智能技术基础系列之:迁移学习

作者:禅与计算机程序设计艺术 1.背景介绍 随着深度学习和神经网络的快速发展,越来越多的应用场景需要处理大量的数据和复杂的任务。然而,由于计算资源、存储空间等条件的限制,传统的人工智能方法往往不能满足实际需求。因此,为了提高效率和降低成本,研究人员提出了迁移学习的概念。本篇文章将详细介绍迁移学习的基

AI天才研究院 699

1033 To Fill or Not to Fill (25point(s))

1033 To Fill or Not to Fill (25point(s)) 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 g

佳迪斯的博客 127

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

1033 To Fill or Not to Fill (25 point(s))

题解 贪心题。每到一个油站,加满油能到达的距离是maxdis, 在此路段之间是否有跟便宜的油站。如果有则在原站点只需加油加到能到达此站点。否则原站点加满,有便宜谁不会占啊,你说对不对。   #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;cstdio&gt; #include&lt;algorithm&gt; us...

徐伯莱 210

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

PAT(甲级)1033 To Fill or Not to Fill (25point(s))

题目 题目链接 思路 题目大意:给出一些加油站以及他们各自油的价格,问如何选择加油策略使得总花费最小; 第一步,我们肯定要将加油站的距离按从小到大的顺序排好; 排好后有一种特殊情况特判一下,如果第一个加油站不在起点,那么这个车就哪也去不了,当然应该直接返回; 接下来考虑策略的选择;对一个加油站i来说,我们肯定会去看在当前加油站i加满油的情况下最远到达的加油站内有没有比i更便宜的,这里分为三种情况: 1、如果有比i更便宜的加油站 j (注意,这里需要选第一个比i便宜的加油站),我们就要看此时油箱中的油能不能到

hjyp的博客 209

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

1033 To Fill or Not to Fill (25 point(s))

思路: 分治和贪心算法,在给定范围[begin, end]内,查找价格最低的加油站;如果没有找到,可以往前 [begin, begin-A*C]的范围内找。 1034 Head of a Gang (30 point(s)) One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatA...

专栏 132

1033 To Fill or Not to Fill (25)(25 point(s)(cj)

1033 To Fill or Not to Fill (25)(25 point(s)) 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 stat...

Cute_jinx的博客 335

1033 . To Fill or Not to Fill (25) (想明白了没写出来,参考吧)

http://blog.youkuaiyun.com/lv_zj/article/details/16115569   #include #include #include using namespace std; class CA { public:  class cstation  {  public:   int point,addlen;   double price;

rightwi的专栏 741

1033 To Fill or Not to Fill (25分)(有空重写一次

#include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const int maxn = 1000; struct Oil { double prize, dis; }a[maxn]; bool cmp(Oil a,...

Eo2s 243

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

qq13754113543的博客 231

PAT 甲级 1033  To Fill or Not to Fill

1033 To Fill or Not to Fill (25 point(s)) 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...

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

1033 To Fill or Not to Fill (25分)

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

pan_xi_yi的博客 243

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

PAT A 1033 To Fill or Not to Fill (25 分)

一、思路 (一)最低油价方案: 1.若前面有一个价格比本站便宜的油站,那就去那边加更便宜的油,且只在本站加够到达低价油站的油; 2.若前面油站都比本站要贵,说明本站的油是当前最便宜的,要把油加满,下一个目的地的前面一段路上相对最便宜的油站。 (二)逻辑: 有两种情形:下一个目的地是油站,或下一个目的地是终点。 1.下一个目的地是油站的情况: (1)若前方存在价格更便宜的油站,且在可达范围内,且比终...

Siumai's Blog 217

【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
上一篇: 1104 Sum of Number Segments (20 point(s))
下一篇: 1060 Are They Equal (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值