PAT 甲级 1033 To Fill or Not to Fill (25 分)

题目描述

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.

思路

乍一看有点难,但其实想通了就是一个贪心算法。我们首先根据加油站距离进行排序,在从一开始到目的地时可以想象这其中每一个加油站都会遇到,我们即可以选择最便宜的。在这样一个前提下,我们每次到达一个加油站要做两件事,计算从当前加油站出发到最大距离中是否有更便宜的加油站(注意找到一个更便宜即可,而不是找到最便宜的,也就是距离最近且比当前加油站便宜的即可),若找到,我们只需要保证当前油能跑到那个加油站即可,那么这时候可以算算当前油还有多少,若油够的话,不需要加油,直接跑到下一个即可,若不够,补充到刚刚能跑到的即可。若没找到,也就是说在其可到达的加油站间,更便宜的,那么我们就把油给加满,然后跑到下一个加油站即可。过程中注意终点即可。

代码

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct node {
	double price;
	double dis;
}Station;
Station sta[1000];
bool cmp(Station a, Station b)
{
	return a.dis < b.dis;
}
int main()
{
	double C, eve, av;
	int N;
	cin >> C >> eve >> av >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> sta[i].price >> sta[i].dis;
	}
	sort(sta, sta + N, cmp);
	double start = 0;
	double all = 0;
	double cur = 0;
	if (sta[0].dis != 0)
	{
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}
	for (int i = 0; i < N; i++)
	{
		double next = start + C * av;
		double mmin = min(next, eve);
		int nextSta = -1;
		int flag = 1;
		for (int j = i + 1; sta[j].dis <= mmin && j<N; j++)
		{
			flag = 0;
			if (sta[j].price < sta[i].price)
			{
				nextSta = j;
				break;
			}
		}
		if (flag == 1 && mmin<eve)
		{
			printf("The maximum travel distance = %.2lf\n",next);
			return 0;
		}
		//
		if (nextSta != -1)
		{
			double gap = sta[nextSta].dis - sta[i].dis;
			double can = cur * av;
			double need = gap - can;
			if (need > 0)
			{
				all += need * sta[i].price / av;
				cur = 0;
			}
			else
			{
				cur = cur - (gap / av);
			}
			start = sta[nextSta].dis;
			i = nextSta - 1;
		}
		else
		{
			if (mmin < eve)
			{
				double man = C - cur;
				all += man * sta[i].price;
				cur = C - (sta[i + 1].dis - sta[i].dis) / av;
				start = sta[i+1].dis;
			}
			else
			{
				all += sta[i].price * (mmin - start) / av;
				printf("%.2lf\n", all);
				return 0;
			}
			
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值