题目1437:To Fill or Not to Fill 题目25

这道题是道比较难的题目,最好先在纸上模拟一遍,需要注意的点有:

1.若0点上没有加油站,车子开始油量是空的,会没法走。这种情况直接判断即可

2.在写代码的时候讲终点也设置为了一个加油站,价格=0,从而方便判断

3.关于在哪个加油站停留并是否加满油的问题:设当前加油站为i,车加满油最多跑M,查看在M距离内i下面的加油站,

如果能找到第一个价格比i低的站设为j,则在i点的加油量正好能到达j即可

如果找不到,说明i点价格最低,在i点加满油。以此类推

题目描述:

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
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
struct Station 
{
	float fPi;
	float fDi;
	Station()
	{
		fPi=0;fDi=0;
	}
};
bool cmp(Station a,Station b)
{
	return a.fDi<b.fDi;
}
int main()
{ 
	int Cmax,D,Davg,N;
	while(scanf("%d %d %d %d",&Cmax,&D,&Davg,&N)!=EOF)
	{
		Station *s=new Station[N+1];
		int i=0;
		for (i=0;i<N;i++)
		{
			cin>>s[i].fPi>>s[i].fDi;
		}
		s[N].fPi=0;s[N].fDi=D;
		sort(s,s+N,cmp);
		int iPoint=0;
		if (s[iPoint].fDi!=0)
		{
			printf("The maximum travel distance = 0.00\n");
			continue;
		}
		float fPri=0,fc=0;
		bool bCannot=false;
		while(iPoint<N)
		{
			if (s[iPoint+1].fDi-s[iPoint].fDi > Cmax*Davg)
			{
				bCannot=true;
				float fdd=s[iPoint].fDi+Cmax*Davg;
				printf("The maximum travel distance = %.2f\n",fdd);
				break;//下一站没发到达!!!!!
			}
			int i=iPoint+1;
			bool iFind=false;
			//寻找第一个价格小于当前价格的点
			while(s[i].fDi-s[iPoint].fDi <= Cmax*Davg && i<=N)
			{
				if (s[i].fPi<s[iPoint].fPi)
				{
					iFind=true;
					break;
				}
				i++;
			}
			if (iFind)//如果找到了,加油量只要够到那个点即可
			{
				fPri=fPri+( (s[i].fDi-s[iPoint].fDi)/(float)Davg-fc )*s[iPoint].fPi ;
				iPoint=i;
				fc=0; 
			}
			else//如果没找到,加满油
			{
				//从能够到达的距离中选出油价最便宜的点
				i=iPoint+1;
				int min=iPoint+1; 
				//查找在能到达的范围内价格最小的点
				while(s[i].fDi-s[iPoint].fDi <= Cmax*Davg && i<=N)
				{
					if (s[i].fPi<s[min].fPi)
											min=i;
					}
					i++;
				}
				fPri=fPri+(Cmax-fc)*s[iPoint].fPi ;
				fc=Cmax-(s[min].fDi-s[iPoint].fDi)/(float)Davg;
				iPoint=min;
				
			}
		}
		if (!bCannot)
		{
			printf("%.2f\n",fPri);
		}
 
	}
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值