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

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

题目大意

首先吐槽一下,这题太有趣了… 题目不长,信息满满… 我还记得在codeup上做过,codeup的测试比pta上严格。
给定若干加油站的油价、与杭州的距离,问能否驾驶汽车行驶到终点。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则计算最远能够行驶多长距离。

思路

首先将加油站按与杭州的距离由近到远排序。判断汽车是否能够行驶到终点。汽车无法行驶到终点有两种情况:一是起点没有加油站,汽车无法启动;二是汽车加满油后能行驶的距离小于两个加油站间的距离。前者最大距离为0,后者最大距离即当前加油站与杭州的距离加上汽车在该加油站加满油后能行驶的距离。
因此,每走到一个加油站,更新加满油后能行驶的总距离max,若max小于下一个加油站与杭州的距离,则油不够,退出循环。
接下来进行下一个加油站的选择,记录当前汽车(未在当前加油站加油)所能行驶到的距离为curLen在(curLen,max)这一区间内寻找是否有更便宜的加油站j,若能找到,则无需加满油,只需加油至能行驶到sta[j].dis(若curLen>sta[j].dis,则油量足够,一滴也不许加);若不能找到,则在当前加油站加满油,更新各参数。注意如果到了最后一个加油站,无需进行下一个加油站的选择,直接更新total为行驶到终点D所需要的花费。
思路虽然没那么复杂,关键在于更新参数时一定要头脑清晰呀!

AC代码

#include<iomanip>
#include<iostream> 
#include<algorithm>
using namespace std;

struct station{
	double price, dis;
	//单位汽油价格和该站到杭州的距离
}sta[510];

bool cmp(station a, station b){
	return a.dis < b.dis;
}

int main(){
	double cmax;//罐的最大容量
	double D;//杭州到目的地的距离
	double Davg;//汽车每单位汽油行驶的距离
	int N;//加油站的总数
	while(cin>>cmax>>D>>Davg>>N){
		double total = 0;//最小花费
		double curLen = 0;//当前汽车可行驶到的距离
		double max = 0;//加满油后最多可行驶到的距离 
		double maxdis = 0;//最长距离 
		bool flag = true;//true表示能行驶完全程 
		for(int i = 0; i < N; i++)
			cin>>sta[i].price>>sta[i].dis;
		sort(sta, sta + N, cmp);
		int i = 0;
		if(sta[i].dis!=0)//起点没有加油站
			flag = false;
		else{
			double dist;//两加油站距离
			for(; i < N; i++){
				max = sta[i].dis + cmax*Davg;
				double next;//下一个点的位置 
				if(i == N-1) next = D;
				else next = sta[i+1].dis;
				if(max < next){//加满油都不够
					flag = false;
					break; 
				}
				else{
					if(max > D) max = D;	
					if(i==N-1){//最后一个加油站
						total += (max - curLen)/Davg*sta[i].price;
						break; 
					}
					int j = i+1, f = 0;
					while(sta[j].dis < max && j<N){
						if(sta[j].price < sta[i].price){
						//找到了更便宜的加油站	
							if(curLen < sta[j].dis){
							//油量不够,需加油 
								total += (sta[j].dis-curLen)/Davg*sta[i].price;
								curLen = sta[j].dis;
							}
							f = 1;
							break;
						}
						j++;
					}
					if(f==0){//未找到更便宜的加油站,加满油 
						total += (max - curLen)/Davg*sta[i].price;
						curLen = max;
					}
				}
			}		
		}
		if(flag)
			cout<<fixed<<setprecision(2)<<total<<endl;
		else{
			cout<<"The maximum travel distance = ";
			cout<<fixed<<setprecision(2)<<max<<endl;
		}		
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值