【贪心】PAT 1033. To Fill or Not to Fill (25) code up To Fill or Not to Fill

1033. To Fill or Not to Fill (25)

声明:本文转自大佬的博客,博主自己拜读后写一下自己的想法
传送门:传送门
时间限制
10 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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

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

需求分析:

这道题目的解题策略是用贪心的思想,目的是到达目的地花最少的汽油钱,根据题目所描述的信息,开始分析一下第一个测试用例

  1. 首先可以将加油站的距离信息从小到达排序,发现了一个特殊情况当初始位置没有加油站的时候直接按照题目给的输出就行;
  2. 在这里插入图片描述根据输入的汽车信息,得到油箱满由的时候可行驶的最大距离,从始发站以这个最大距离作为范围,开始遍历范围内的加油站,由于 我们要十分的贪心 所以我们要找价格比当前加油站低的 作为下一个落脚点,--------作为情况一(如果有多个便宜的,选择最近的哪一个),如果范围内没有加油站价格比当前的低,就选择相对来讲最便宜的作为下一个落点------作为情况二。----------------------这里就会出现一种特殊情况:当前加油站范围内没有任何加油站了,所以下一站没有落脚点了,直接把油箱加满能跑多远是多远了。

分析完大概的情况后就可以写代码了。

#include <iostream>
#include <algorithm>
using namespace std;
struct stations{
	double price;
	double distance;
}sta[1010];
bool cmp(stations a,stations b)
{
	return a.distance<b.distance;
}
int main()
{
	int tank,dis,perDis,n;
	double maxDis;
	double sumMoney=0;
	bool flag=true;
	while(scanf("%d%d%d%d",&tank,&dis,&perDis,&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&sta[i].price,&sta[i].distance);
		}
		sort(sta,sta+n,cmp);
		maxDis=tank*perDis;
			if(sta[0].distance!=0)
			{
				printf("The maximum travel distance = 0.00\n");
				continue;
			}
			if(dis==0)
			{
				printf("0.00\n");
				break;
			}
		int curNum=0;
		double curgas=0;
		int i;
			while(flag)
			{
				bool tag=false;//最 大范围内是否有加油站 
				bool ifcheaper=false;//范围内是否有比当前加油站更便宜的加油站 
				int cheaperNum;//范围内没有比当前加油站更便宜的加油站, 
				double cheaperPrice=10000;
				for(i=curNum+1;i<n;i++)
				{
					if(sta[curNum].distance+maxDis>=sta[i].distance) 
					{
						tag=true;
						if(sta[i].price<sta[curNum].price)
					{
						ifcheaper=true;
						double needgas=(sta[i].distance-sta[curNum].distance)/perDis-curgas;
						sumMoney+=needgas*sta[curNum].price;
						curNum=i;
						curgas=0;
							printf("%.2lf\n",sumMoney);
						break;
					}
					
					if(sta[i].price<cheaperPrice)
					{
						cheaperPrice=sta[i].price;
						cheaperNum=i;
					}
					}
					else break;
				
				}
				
				if(!ifcheaper&&(maxDis>=dis-sta[curNum].distance))
				{
					double needgas=(dis-sta[curNum].distance)/perDis-curgas;
					sumMoney+=needgas*sta[curNum].price;
					printf("%.2lf\n",sumMoney);
					return 0;
				}
				if(tag&&!ifcheaper)
				{
					double needgas=tank-curgas;
					sumMoney+=(needgas*sta[curNum].price);
					curgas=tank-((sta[cheaperNum].distance-sta[curNum].distance)/perDis);
					curNum=cheaperNum;
						printf("%.2lf\n",sumMoney);
				}	
				else if(!tag)
				{
				 printf("The maximum travel distance = %.2lf\n",sta[curNum].distance+maxDis);
					return 0;
				}
			}
			
			
		
		}
		
		
	
}

博主能力有限(其实就是个辣鸡)自己了解了大概的情况也写不对,只好参考了大佬的代码,有研究了好久,也得到了一点小点子:

  1. 这里的tag 和 ifcheaper 有着很大的最用,这就根据这两种的值就可以判断是情况一还是情况二,情况一就直接加油,加油量是正好到达下一个落脚点,算钱,改变当前落脚点,
  2. 情况二:先记录下下一个落脚点的 i ,就是第几个加油站,然后在当前加油站巴油加满(因为当前加油站是范围内最便宜的了,直接加满到后面贵的由少加,贪心),所以到下一个落脚点的时候油箱内可能由汽油剩余,这是要记录好油箱的剩余油量,用于下一次加油时的计算。

本博客只是笔者学习时候的小随笔,如果有错误请足下赐教,希望可以共同进步。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值