PTA-甲级 1033 To Fill or Not to Fill(测试点2)

1033 To Fill or Not to Fill (PAT Advanced) 坑点 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... 阅读详情

PTA-甲级 1033 To Fill or Not to Fill(测试点2)

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

知识点: 贪心算法

算法:

if 最近一站距离<cmax*davg
	去不了终点了;
	break;
else 在能到达范围内,
	if 存在更低油价
		选择最近的油价更低的作为下一站;
	else if 能到终点了
		去终点;
	else 去能到达的最远的一站;

注意点:

1.不要直接用测试里的12、600的数据…这是输入进来的不是自带的(修正后测试点1、6过了)

2.测试点2是第一个站不在原点的情况,直接输出 The maximum travel distance = 0.00

3.测试点4错误可能是到终点的情况出现了问题(猜测)

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int cmax,d,davg,n;
double total;
struct stu{
	int dis;
	double pri;
} sta[505];
bool comp(stu a, stu b){
	return a.dis<b.dis;
}
int main(){
	total=0.0;
	double cont=0.0;
	double distance=0.0;
	bool end=false;
	cin>>cmax>>d>>davg>>n;
	for(int i=0; i<n; i++){
		cin>>sta[i].pri>>sta[i].dis;
	}
	sta[n].dis=d;
	sta[n].pri=0;
	sort(sta, sta+n, comp);
	for(int i=0; i<n; i++){
		if(sta[i+1].dis-sta[i].dis>cmax*davg){
			distance+=cmax*davg;
			break;
		}
		for(int j=i+1; j<=n; j++){
			if(sta[j].dis-sta[i].dis>cmax*davg){
				total+=sta[i].pri*(cmax-cont); 	// 加满 
				cont=(cmax-(sta[j-1].dis-sta[i].dis)*1.00/davg)*1.00; 	// 去完下一站剩的油 
				i=j-2; 	//下一站
				distance=sta[j-1].dis; 
				break;
			}
			else if(j==n){
				total+=sta[i].pri*(sta[j].dis-sta[i].dis-cont*davg)/davg;
				cont=0; 	// 到终点油用完 
				end=true; 	// 到终点了 
				distance=d; 
				break;
			}
			else if(sta[j].pri<sta[i].pri){
				total+=sta[i].pri*(sta[j].dis-sta[i].dis-cont*davg)/davg;
				cont=0; 	//到一站油刚好用完 
				i=j-1;	// 下一站 
				distance=sta[j].dis; 
				break;
			}
		}
		if(end){
			break;
		}
	}
	if(sta[0].dis!=0){
		printf("The maximum travel distance = 0.00\n");
	}
	else if(end){
		printf("%.2f\n", total);
	}
	else{
		printf("The maximum travel distance = %.2f\n", distance);
	}
}
【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 阅读详情

相关推荐

ElasticSearch读写性能调优

深层次理解,虽然是每隔一秒就会将内存中的segment数据刷新到segment file中,但实际上这里并没有直接到磁盘文件,而是先写入到OS Cache中。以refresh_interval为间隔时间,将保存在ES 内存缓存区中的数据 刷新到 os的文件系统缓存中,定期清空es的buffer,生成segment。注意:ES 的默认设置,已经综合考虑了数据可靠性,搜索的实时性,写入速度,一般不要盲目修改。将segment刷新到os cache中,并开放了查询权限,以提升搜索的实时性。

qq_44027353的博客 1812

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

Pytorch中CrossEntropyLoss()详解

交叉熵主要是用来判定实际的输出与期望的输出的接近程度。举个例子:在做分类训练的时候,如果一个样本属于第 K 类,那么这个类别所对应的输出节点的输出值应该为1,而其他节点的输出都为0,即 [0,0,1,0,….0,0] ,这个数组也就是样本的 Label,是神经网络最期望的输出结果。我们用交叉熵损失函数来衡量网络的实际输出与正确标签的差异,利用这种差异经过反向传播去更新网络参数。交叉熵:它主要刻画的是实际输出(概率)与期望输出(概率)的距离,也就是交叉熵的值越小,两个概率分布就越接近。

flyingluohaipeng的博客 3865

PAT甲级 1033 To Fill or Not to Fill (测试点2、4)

PAT甲级1033题

blueblue0808的博客 942

中文翻译《ASPICE in practice》之“SUP.8 配置管理”

一般来说,自动化程度越低,负责 CM 的个人就必须花费越多的精力,例如,检查基线的正确性和完整性,或确保在归档系统上正确完成手动版本控制。必须防止未经许可的访问。为了唯一地标识基线,许多 CM 工具提供了一种“标记”基线的机制,即为受影响的 CM 项目的所有版本分配相同的标签,例如“第 3 次交付状态”。此选择必须允许绘制合格的基线(参见 BP5),即确保实际上所有描述特定开发阶段的元素(需求和设计文档、开发环境、变更请求、测试用例、测试文档,也可能是重要的中间工作产品等)都可以在 CM 系统中进行基线。

Smith_Chai的博客 1594

Python量化交易实战:pytdx库高效获取通达信分笔成交数据

本文详细介绍了如何使用Python的pytdx库高效获取通达信分笔成交数据,为量化交易提供关键市场微观结构分析。通过环境搭建、服务器连接、数据获取与清洗的完整流程,帮助开发者快速掌握这一免费高质量数据源,并实现分笔数据的实战分析与应用。

weixin_29061509的博客 1384

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

宝付支的博客 393

51单片机最强模块化封装(3)

本篇文章将带大家继续封装我们的代码。这里我们会封装数码管的操作函数。我们又封装了一个模块啦,不知道现在大家有没有感受到模块化的强大呢。B站。

m0_49476241的博客 4065

PAT-AL 1033. To Fill or Not to Fill

1、知识点:贪心 2、思路:先将站按照距离从小到大排序。假设我有N个桶,第i号桶装第i站的汽油(i从0开始),第0站加满,到第i站时,选择从第i-1站到达此站最便宜的汽油(如果最便宜的不够用,选择次便宜的),然后将N个桶中所有比此站更贵的汽油原价卖出(说明之前买的多了,够用就好),然后加此站的油至加满(不管是不是比之前或后面的更贵,因为不加满怕到不了下个站)。最后到达终点时将所有剩下的油原价卖出即

EverydayIsLife的博客 438

SAP中固定资产的减值处理(通常称为“资产减值”或“价值调整”)主要遵循财务会计准则(如IFRS或本地GAAP)

摘要:SAP系统中固定资产减值处理通过专门的事务类型(如ABAW)实现,遵循IFRS或本地GAAP准则。处理流程包括减值测试、可收回金额确定、减值金额计算及会计过账,减值金额计入KAAFA字段。核心后台表包括ANLA(资产主数据)、ANLC(存储减值累计值)、ANEP(减值明细)和ANEK(凭证抬头),其中ANLC表的KAAFA字段记录减值准备总额,ANEP表通过BWASL字段识别减值交易。减值会影响后续折旧计算,且在某些准则下允许减值回转(事务代码ABAU/ABAX)。

金牌架构师 969

系规备考论文:论IT服务规划设计

系规备考论文:论IT服务规划设计

liuxu324的博客 1149

PAT甲级 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. Dif

changeforeve的博客 207

【以太网硬件一】802.3xx标准怎么这么多?它们有什么关系?

IEEE 802.3标准发展历史简介

highman110的博客 5023

PAT 乙级-1033(测试点2)

测试点2:可能没有坏键,此时输入的第一行为空格,第二行为应该输入的文字,使用getline可以解决问题 #include<iostream> #include<cctype> using namespace std; int main() { string broken, ori; getline(cin, broken); getline(cin, ori); int flag = 0; if(broken.find('+') != string::npos) .

学习笔记,不喜勿喷! 280

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

python-PAT甲级题解-1005

Given a non-negative integerN, your task is to compute the sum of all the digits ofN, and output every digit of the sum in English. Input Specification: Each input file contains one test case. Each case occupies one line which contains anN(≤10100). ...

m0_48118006的博客 1236

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

PAT甲级 2019年冬季 题解

题目1:7-1 Good in C (20分) When your interviewer asks you to write “Hello World” using C, can you do as the following figure shows? Input Specification: Each input file contains one test case. For each case, the first part gives the 26 capital English letters

我是没有梦想的小白 936

PAT甲级2014秋季全解

PAT甲级2014秋季1. Broken Keyboard2. Perfect Sequence3. Tree Traversals Again4. All Roads Lead to Rome 1. Broken Keyboard On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not a

all23333333的博客 388

2021年秋季PAT甲级考试满分题解

7-1 Arrays and Linked Lists (20 分) Let’s design a data structure AAA that combines arrays and linked lists as the following: At the very beginning, an integer array A0A_0A0​ of length L0L_0L0​ is initialized for the user. When the user tries to access the

weixin_44589651的博客 1664

PAT(Advanced)甲级---1114 Family Property (25 分)【DFS,并查集】

This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each f...

啸天的博客 365

《算法笔记》3.1小节——入门模拟 问题 E: Shortest Distance (20)

题目描述 The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. 输入 Each input file contains one test ca...

hwyu_95的博客 282

python-PAT甲级题解-1001

Calculatea+band output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Specification: Each input file contains one test case. Each case contains a pair o..

m0_48118006的博客 310

【PAT甲级】1164 Good in C (20 分)

PAT甲级1164题解

小染的学习笔记 749

1033 To Fill or Not to Fill (25 分)测试点0、1

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 di

weixin_45050148的博客 404

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

haohao0626的博客 573

2021-11-04 PAT(6) 从简单题过渡到中等题

最近没做PAT了,有些忙,打算一个月激情冲刺PAT,目前就是偶尔做题。 1094 The Largest Generation (25 分) A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest po

Yiyan__的博客 749

【PAT甲级】1033 To Fill or Not to Fill (25分)

解题过程的小记录,如有错误欢迎指出。 难度:四星 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 注意点 我的解题过程 思路 bug 一开始毫无思路,无从着手 代码 dalao的代码 全部代码因版权原因不放出来,大家可以自行去柳神博客购买或者参考晴神的上机笔记~ 借鉴点 ...

SpicyChicken的自我修养 351
上一篇: PTA甲级-1057 Stack (超时)
下一篇: PTA-甲级 1094 The Largest Generation 深度优先
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值