1033 To Fill or Not to Fill (25 分)

题目

题目链接

题解

贪心。

1. 将终点视为单位油价为0,离起点距离为d的加油站,然后将所有加油站按离起点的距离从小到大排序。排序后,如果如果离起点最近的加油站距离不是0,则汽车无法出发,直接输出结果。
2. 选择下一个车站的策略:
	判断是否存在可达的加油站,即将油箱加满能够到达其他加油站?
	(1)若存在,则判断是否存在比当前加油站油价便宜的加油站?
			① 若存在,则我们选择一个最近的且比当前加油站油价便宜的加油站作为目的地,将油箱加至刚好能到目的地,驱车前往;
			② 若不存在,则将油箱加满,驱车前往能到达的加油站中油价最便宜的加油站;
	(2)若不存在,则说明无法到达终点

代码

#include<bits/stdc++.h>
using namespace std;
#define pos first
#define price second
#define PDD pair<double, double>

const int N = 1e5+10;

double V, dist, com;
int n;

PDD sta[N];

int main()
{
	cin >> V >> dist >> com >> n;
	
	for (int i = 0;i < n;i ++) {
		double x, y;
		cin >> x >> y;
		sta[i] = {y, x};
	}	
	sta[n] = {dist, 0.0};
	sort (sta, sta+n);
	
	if (sta[0].pos != 0) return printf ("The maximum travel distance = 0.00\n"), 0;
	
	int i = 0;
	double Vdist = V * com;
	double cost = 0, cur_V = 0;
	while (i < n) { // !!不能枚举终点
		
		int j = i+1, t = -1;
		while (j <= n && sta[j].pos <= sta[i].pos + Vdist) {
			if (t == -1 || sta[j].price <= sta[t].price) t = j;
			if (sta[t].price <= sta[i].price) break;
			j ++;
		}
		if (t == -1) return printf ("The maximum travel distance = %.2lf\n", sta[i].pos + Vdist), 0;
		
		if (sta[t].price <= sta[i].price) { // 正好 
			if (sta[t].pos <= sta[i].pos + cur_V * com) { // 当前油箱中的油足够 
				cost += 0;
				cur_V -= (sta[t].pos - sta[i].pos) / com;
			} else { // 不够,缺的量要补上 
				cost += ((sta[t].pos - sta[i].pos) / com - cur_V) * sta[i].price;
				cur_V = 0;
			}
		} else { // 加满 
			cost += (V - cur_V) * sta[i].price;
			cur_V = V - (sta[t].pos - sta[i].pos) / com;
		}
		
		i = t;
	}
	
	printf ("%.2lf\n", cost);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不牌不改

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

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

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

打赏作者

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

抵扣说明:

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

余额充值