Codeup/PAT To Fill or Not to Fill(贪心思想)

 To Fill or Not to Fill

时间限制: 1.000 Sec  内存限制: 32 MB

题目描述

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

其他测试数据:

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

分析:首先判断特殊情况,也就是汽车开不到终点的情况。

1.在起点处没有加油站;

2.在路程中,最近两个加油站的距离大于汽车加满油能走的距离;

这里有个技巧,如果判断出这两个特殊情况,可以直接return 0,结束程序

如果汽车可以开到终点,采取以下策略。

1.在汽车加油能到的范围内,尽量先加油到更便宜的加油站去加油。(即加刚刚好的油支持汽车开到更便宜的加油站

2.如果在汽车加油能到的范围内,不存在比汽车现在所在加油站还便宜的,则在此处加满油,并且选择下一个次便宜的加油站(即,在汽车能到的加油站内选最便宜的)

(因为是先加满油,所以当到下一个加油站时,汽车可能还有剩余油量,这个剩余油量是要考虑在内的)。

对测试数据更具体的分析见下面博客:

https://www.cnblogs.com/XBWer/p/3866486.html

代码:

struct gas{
	double price;
	double Long;
}s[30005];
bool cmp(gas s1,gas s2){
	return s1.Long<s2.Long;
}
int main(){
	double C,D,Davg;    //这里用double更方便
	int N;
	scanf("%lf %lf %lf %d",&C,&D,&Davg,&N);
	for(int i=0;i<N;i++)
		scanf("%lf %lf",&s[i].price,&s[i].Long);
	s[N].price=0,s[N].Long=D;    //终点做成最后一个加油站,但是价格最便宜,保证最后可以直接加合适的油到终点;
	sort(s,s+N,cmp);
	double sumPrice=0.0; 
	if(s[0].Long!=0){
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	
	double maxLong=Davg*C;	
	double sumLong=0;
	double sumGas=0;    //汽车内剩余油量
	for(int i=0;i<N;){
		if(s[i+1].Long-s[i].Long>maxLong){
			printf("The maximum travel distance = %.2f",(double)s[i].Long+maxLong);
			return 0;
		}
		
		int f=1;
		for(int j=i+1;j<=N;j++){
			double distance=s[j].Long-s[i].Long;	 
			if(distance<=maxLong&&s[j].price<=s[i].price){	//目前加油站能到达的位置,并且价格比目前的便宜,就先开到便宜的地方 
				sumLong=s[j].Long;
				sumPrice+=1.0*(distance-sumGas*Davg)/Davg*s[i].price;
				sumGas=0;    //每次有更便宜的加油站,那么到该加油站的油必然是刚好合适
				i=j;   
				f=0; 
				break;			
			}
		}
		//如果不存在比目前便宜加油站,选择能开到的最便宜的加油站
		if(f){
			sumPrice+=s[i].price*(C-sumGas);
			double tempLong=maxLong+sumLong; 
			double minPrice=s[i+1].price;
			double minLong=0;
			double minj=0; 
			for(int j=i+1;j<N;j++){
				if(s[j].Long<=tempLong){
					if(s[j].price<=minPrice){
						minPrice=s[j].price;
						minLong=s[j].Long;
						i=j;//
					}
				}
			}
			sumGas=1.0*(maxLong-(minLong-sumLong))/Davg;	//剩余油量 
			sumLong=minLong;		//目前的距离
		}
	}
	printf("%.2f",sumPrice);
	return 0;
}

注意:PTA上的测试数据不够全面;一开始在汽车能开到终点的第二条选择策略上,采取直接开到最远的加油站的方法,在pta上通过了,当是实际上想法时不全面的。

参考测试数据:

50 1300 12 8
7.10 0
7.00 150
7.50 400
8.00 600
7.20 900
7.30 1000
6.00 1250
6.85 1280
-------------------
正确答案为   767.50

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值