题目1437:To Fill or Not to Fill

题目1437:To Fill or Not to Fill

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1040

解决:223

题目描述:

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.

输入:

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.

样例输入:
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
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
来源:
2012年浙江大学计算机及软件工程研究生机试真题

题目思路:
1、始发点必须有加油站,结束
2、如果当前点加满也到不了下个点(包含终点),能走多远走多远,结束
3、寻找当前点A方圆内最便宜的,如果A即为最便宜的,则如果A可以到终点,则加到终点油量即可,出循环,如果
A不可到终点,则加满,去往下一个加油站;如果A方圆内B为最便宜的,则直接加油加到刚好可以到B,跳到B

c++代码:
//
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x7FFFFFFF
#define ZERO 1e-8
//#define min(x,y) (x)<(y)?(x):(y)
const int MAX = 510;
struct node
{
	double pos;
	double price;
	bool operator < (const node &A)const
	{
		return pos<A.pos;
	}
};

double Cmax;
double Davg;
double Dis;
double currentGas;
double currentDis;
double maxd; 
double cost;
int n;
node tank[MAX];

int
main(void)
{
	while(cin>>Cmax>>Dis>>Davg>>n)
	{
		for(int i=0; i<n; ++i)
		{
			scanf("%lf%lf",&tank[i].price,&tank[i].pos);
		}
		sort(tank,tank+n);
		
		tank[n].pos = Dis;
		tank[n].price = INF;

		currentGas = 0;
		currentDis = 0;
		maxd = Cmax*Davg;
		
		cost = 0;
		if((tank[0].pos-0.00) > 1e-8)//测试点 
		{
			//不能出发 
			printf("The maximum travel distance = %.2lf\n",currentDis);
			continue;//返回 
		}
		int i;
		bool flag = false;
		for(i=0; i<n&&!flag; ++i)
		{
			currentDis = tank[i].pos;
			if(maxd < nextd)
			{
				//不能到达下站能走多远走多远 
				printf("The maximum travel distance = %.2lf\n",currentDis+maxd);
				break;
			}
			else
			{
				double mins = tank[i].price;
				int minp = i;
				for(int j=i+1; j<n; ++j)
				{
					if(tank[j].pos>tank[i].pos+maxd)
					{
						break;
					}
					else if(tank[j].price<mins)
					{
						mins = tank[j].price;
						minp = j;
						break;
					}
				}
				if(i==minp)
				{
					//最便宜的了
					 double tp = min(tank[n-1].pos-tank[i].pos,maxd);
					 if(tp==maxd && maxd!=(tank[n].pos-tank[i].pos))
					 {
					 	//加满
						 double need = Cmax- currentGas;
						 cost += need*tank[i].price;
						 currentGas = Cmax-(tank[i+1].pos-tank[i].pos)/Davg;
					 }
					 else
					 {
					 	double need =  (tank[n].pos-tank[i].pos)/Davg;
					 	if(need > currentGas)
					 	{
					 		cost += (need-currentGas)*tank[i].price;
					 	}
					 	flag = true;
					 	break;//到了 
					 }
				}
				else
				{
					double tp = tank[minp].pos-tank[i].pos;
					double need = tp/Davg;
					if(need > currentGas)
					{
						cost += (need-currentGas)*tank[i].price;
						currentGas = 0;
					}
					else
						currentGas -= need;
					i = minp-1;
				}
			}
		}
		if(flag==true)
		//打印cost 
			printf("%.2lf\n",cost);
	
	}
}


std::fill是一个在C++中用于将容器中的元素置为指定值的函数。引用提供了一种使用std::fill将vector中的元素置为零的方法。首先,我们需要创建一个包含n个元素的vector对象v,初始值都为0。然后,使用std::fill(v.begin(), v.end(), 0)将v中的所有元素都置为0。这种方法是一种比较快速的置零vector元素的方式。 除了使用std::fill,还可以使用其他方法将vector中的元素置为零。引用提供了几种其他的方法。一种是使用iterator迭代器来逐个将vector中的元素置为零,另一种是使用assign函数将vector中的元素全部赋值为零,还有一种是使用memset函数将vector的内存块置为零。 总结来说,std::fill是一种快速将vector中的元素置为指定值的方法,但也可以使用其他方法实现相同的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ vector中所有元素快速置零](https://blog.csdn.net/qq_42080098/article/details/124356341)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++标准模板(STL)(std::fill)](https://blog.csdn.net/qq_40788199/article/details/127424782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值