2022.10.15每日刷题打卡

旅行家的预算

考虑贪心。

  1. 贪心地选择能到且油价更低的加油站
  2. 若不能直达油价更低的加油站,就加满(加满是为了下一站更贵的加油站加的油少些)油选择一个范围内能到的加油站
  3. 如果任意一个加油站加满油不能到下一个加油站那么答案不存在

 贪心,模拟就可以。

#include <bits/stdc++.h>
#define x first
#define y second
typedef std::pair<double, double> PDD;

const int N = 550;
PDD a[N];

bool cmp(PDD a, PDD b) {
	return a.x < b.x;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	double t, c, d, p1;
	int n;
	std::cin >> t >> c >> d >> p1 >> n;
	a[0].y = p1, a[n + 1].x = t;

	for (int i = 1; i <= n; i++) {
		std::cin >> a[i].x >> a[i].y;
		if (a[i].x - a[i - 1].x > c * d) {
			std::cout << "No Solution\n";
			return 0;
		}
	}

	std::sort(a + 1, a + 1 + n, cmp);

	int i = 0;
	double ans = 0, cur = 0;
	while (i <= n + 1) {
		int j = i + 1, k = i + 1;
		while (j <= n) {
			if (a[j].y <= a[k].y) {
				k = j;
			}
			if (a[i].y >= a[j].y) {
				break;
			}
			if (a[j + 1].x - a[i].x > c * d) {
				break;
			}
			j++;
		}
		if (a[j].y > a[i].y) {
			ans += (c - cur) * a[i].y;
			cur = c - (a[k].x - a[i].x) / d;
			i = k;
		} else {
			ans += ((a[j].x - a[i].x) / d - cur) * a[i].y;
			i = j;
			cur = 0;
		}
	}

	std::cout << std::fixed << std::setprecision(2) <<  ans << "\n";
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值