PTA-To Fill or Not to Fill (加油问题)(贪心)

7-3 To Fill or Not to Fill (30 分)

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 N lines 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 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

附上一张译文翻译
在这里插入图片描述
好了,这个题我们首先来想,如何花费最少!
很简单,加最便宜的油就是了,到终点时油要刚好用完。
那我们按照加油站的地点来排序,由于我们一开始是没有油的,所以如果起点没有加油站的话,我们就直接结束(没油怎么跑嘛)最远距离就是0.
好了,现在我们在起点并且起点有加油站,继续考虑。我们能跑的最大距离是油箱的容量*每升油跑的距离,我们在这个距离范围内搜索,有以下3种情况:
1.找到了比它更便宜的加油站,秉持着加最便宜的油的想法,我们只要有跑到那个更便宜的加油站的油就行了。
2.找不到比它更便宜的油站,那这时候最便宜的油就是自己了,所以我们加满油再跑到价格仅次于我们油站的油站
3.最尴尬的情况就是,在这个距离内我找不到任何一个油站了,意味着就算我加满油也跑不到下一个油站,所以这个时候直接结束,最大距离就是我们现在的距离加最远距离。
所以分析就是这样了,下面上代码:

#include "bits/stdc++.h"
using namespace std;
pair<int,double> t[1000];
int v,d,c,n,j;//j是目前我的油站
double ans,c1,d1;//c1表示我现在的存油,d1表示我目前的距离
bool flag=true;//判断是否找不到油站了
int main()
{
	cin>>c>>d>>v>>n;
	for(int i=0;i<n;i++) cin>>t[i].second>>t[i].first;
	t[n].first=d;t[n].second=0;//将终点也看成油站
	sort(t,t+n+1);//sort会默认由小到大排第一个元素
	if(t[0].first!=0)//起点没有油站
	{
		cout<<"The maximum travel distance = 0.00"<<endl;
		return 0;
	}
	while(d1<d)//当我没跑到终点之前
	{
		int k=j+1;//k为价格仅次于目前的油站
		int next=0;//价格更便宜的油站
		for(int i=j+1;i<=n&&t[i].first<=d1+v*c;i++)
		{
			if(t[i].second<=t[j].second)//找到了便宜的油站
			{
				next=i;
				break;
			}
			if(t[i].second<t[k].second) k=i;
		}
		if(next)//第一种情况
		{
			ans+=((t[next].first-d1)/v-c1)*t[j].second;//加刚好能到的油就行
			j=next;//油站更新
			d1=t[j].first;//距离更新
			c1=0;//油箱无油
		}
		else if(t[k].first<=t[j].first+c*v)//在最大距离内找到了下一个油站    第二种情况
		{
			ans+=(c-c1)*t[j].second;
			c1=c-(t[k].first-d1)/v;
			j=k;
			d1=t[j].first;
		}
		else //找不到油站了    第三种情况
		{
			d1+=v*c;
			flag=false;
			break;
		}
	}
	if(!flag) printf("The maximum travel distance = %.2lf",d1);//走不到终点
	else printf("%.2lf",ans);//最小花费
}

所以,我们解决了这个问题。
思路很重要,有时候思路错误会绕好久的!
我是阡陌,一个在打ACM的业余选手。如果你喜欢这篇博客,就点个赞再走吧!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

indolence-阡陌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值