Codeup100000584问题 C: To Fill or Not to Fill

Codeup100000584问题 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 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。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)。

实现代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;

int inf=100000000;

struct petrol{
	double Pi;	//单位汽油价格
	double Di;	//i = 1,... N时该站点与杭州之间的距离。
}P[510];

bool cmp(petrol a,petrol b){
	return a.Di<b.Di;
}

int main()
{
	double Cmax;	//储罐的最大容量
	double D;		//杭州到目的地城市的距离
	double Davg;	//汽车可以行驶的每单位汽油的平均距离
	int N;			//加油站总数
	while(scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N)!=EOF){
		for(int i=0;i<N;i++){
			scanf("%lf%lf",&P[i].Pi,&P[i].Di);
		}
		P[N].Di=D;
		P[N].Pi=0;
		sort(P,P+N,cmp);
		if(P[0].Di!=0||D==0){
			printf("The maximum travel distance = %.2f",0.00);
		}else{
			int now = 0;    //当前所处的加油站编号
        	double nowTank = 0;	//当前油量
			double cost=0;		//能到达终点时的总花费 
			double dismax=Cmax*Davg;	//满油能行驶的最大距离  
			while(now<N){
				int k=-1;	//选中的下一个站 
				int minprice=inf;	//最小花费
				//循环选出满足要求的下一站的位置 
				for(int i=now+1;i<=N&&P[i].Di-P[now].Di<=dismax;i++){
					if(P[i].Pi<minprice){
						minprice=P[i].Pi;
						k=i;
					}
					//如果下一站的油价小于当前站价格,则该站即为当前站的下个最优站 
					if(minprice<P[now].Pi){
						break;
					}
				}
				if(k==-1){
					break;
				}
				double need=(P[k].Di-P[now].Di)/Davg;	//求出到下个最优站所用的油量
				//如果当前站油价比下个最优站的大,那我们只要加够到下个最优站的油即可 
				if(minprice<P[now].Pi){	
					if(nowTank<=need){ //当前油量不足以到达下个最优站 
						cost+=(need-nowTank)*P[now].Pi; //加到下个最优站差额的油量 
						nowTank=0; //到达下个最优站的油量为0 
					}else{ // 当前油量够到下一站 
						nowTank-=need; //到达下一站的油量为当前油量-所需油量 
					} 
				}else{//若小,则加满油 
					cost+=(Cmax-nowTank)*P[now].Pi;
					nowTank=Cmax-need;
				} 
				now=k;	//到达下一站 
			}
			if(now!=N){//没有到达终点 
				//最大距离为当点站距离加满油状态下的最大行驶距离
				printf("The maximum travel distance = %.2f\n",P[now].Di+dismax); 
			}else{
				printf("%.2f\n",cost);
			}
		}
	}
	return 0;
}
<think>好的,用户想了解如何用std::copy和std::copy替换memcpy和memset。首先,我需要回顾这两个C++标准库函数的具体用法和优势。 用户可能已经知道memcpy和memset是C语言中的函数,用于内存操作。但在C++中,尤其是处理对象时,直接内存操作可能会有问题,比如浅拷贝或未定义行为。std::copy和std::fill属于<algorithm>头文件,更安全,适用于STL容器和数组。 接下来,比较memcpy和std::copy。memcpy需要计算字节数,容易出错,而std::copy通过迭代器指定范围,类型安全。例如,拷贝int数组时,std::copy(arr1, arr1+5, arr2)比memcpy(arr2, arr1, 5*sizeof(int))更不容易出错,特别是当元素类型改变时,不需要手动计算字节。 然后考虑memset和std::fill。memset按字节设置,适合POD类型,但对非POD或非0值可能有风险。比如,用memset初始化int数组为1会导致每个字节为1,而不是整数1。而std::fill会正确调用赋值操作,确保每个元素被正确设置。 用户可能需要具体例子来理解转换方法。比如,将memcpy替换为std::copy时,源和目标的范围需要明确,使用指针作为迭代器。对于memset到std::fill的转换,需注意填充的范围和值类型。 还要提到异常安全性和泛型编程的优势。std::copy和std::fill在拷贝或填充时,能正确处理异常,而memcpy和memset不会。此外,标准算法适用于任何迭代器,包括容器迭代器,提高代码的通用性。 最后,需要指出何时仍需使用C函数。例如,处理原始内存块或与C API交互时,可能仍需memcpy。但在大多数C++场景中,优先使用标准库算法更安全、可维护。</think>在C++开发中,建议优先使用标准库算法$\texttt{std::copy}$和$\texttt{std::fill}$代替C语言的$\texttt{memcpy}$和$\texttt{memset}$,主要原因如下: ### 一、类型安全优势 $$\begin{cases} \texttt{memcpy} \rightarrow \text{按字节复制,无类型检查} \\ \texttt{std::copy} \rightarrow \text{通过迭代器实现类型安全复制} \\ \texttt{memset} \rightarrow \text{按字节填充,仅适用POD类型} \\ \texttt{std::fill} \rightarrow \text{通过值语义进行安全填充} \end{cases}$$ ### 二、具体转换示例 #### 1. 替换$\texttt{memcpy}$ ```cpp // C风格 int src[5] = {1,2,3,4,5}; int dest[5]; memcpy(dest, src, 5 * sizeof(int)); // C++风格 std::copy(std::begin(src), std::end(src), std::begin(dest)); ``` #### 2. 替换$\texttt{memset}$ ```cpp // C风格(仅适用于基础类型) char buffer[1024]; memset(buffer, 0, sizeof(buffer)); // C++风格(支持任意可赋值类型) std::fill(std::begin(buffer), std::end(buffer), '\0'); ``` ### 三、核心优势对比 $$ \begin{array}{|l|c|c|} \hline \text{特性} & \text{C函数} & \text{C++算法} \\ \hline 类型安全 & × & √ \\ 支持非POD类型 & × & √ \\ 异常安全 & × & √ \\ 泛型编程支持 & × & √ \\ 容器兼容性 & × & √ \\ \hline \end{array} $$ ### 四、注意事项 1. 对于连续内存块(如数组),使用$\texttt{std::copy}$性能与$\texttt{memcpy}$相当 2. 处理复杂对象时,$\texttt{std::copy}$会调用拷贝构造函数 3. 使用前需包含头文件:$<\texttt{algorithm}>$ ### 五、仍需使用C函数的场景 1. 与C语言API交互时 2. 处理未初始化的原始内存块(配合$\texttt{std::mem\_t}$) 3. 需要极致的底层性能优化时(需通过profile验证) 建议优先使用标准库算法,可使代码更安全、更易维护,同时保持与STL容器良好的兼容性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值