1033 To Fill or Not to Fill (25分)

真复杂。。。

#include<iostream>//AC//建议看算法笔记
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 550;
double gasCap, dsum, disPerGas;
int n;
struct node {
	double price, d;
};
bool cmp(node a, node b) { return a.d < b.d; }
vector<node> sta;

int main() {
	cin >> gasCap >> dsum >> disPerGas >> n;
	double maxLen = gasCap * disPerGas;
	for (int i = 0; i < n; i++) {
		double a, b;
		cin >> a >> b;
		sta.push_back({ a,b });
	}
	sta.push_back({ 1e9,dsum });
	n++;
	sort(sta.begin(), sta.end(), cmp);
	if (sta[0].d > 0) {
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	int p = 0;
	double ans = 0, leftGas = 0;
	while (p < n - 1) {
		int next = p + 1;
		if (maxLen < (sta[next].d - sta[p].d)) {
			double maxD = sta[p].d + maxLen;
			printf("The maximum travel distance = %.2f", maxD);
			return 0;
		}
		double minPrice = sta[p].price, minSta = -1;
		while (next < n && sta[next].d - sta[p].d <= maxLen) {
			if (sta[next].price <= minPrice) {
				minPrice = sta[next].price;
				minSta = next;
				break;///到比自己低的(注意不是选最低,而是选比自己低的里面离自己最近的那个)
			}
			next++;
		}
		if (minSta == -1) {//最大量
			double l = min((dsum - sta[p].d), maxLen);
			for (next = p; next < n; next++) {
				if (l + sta[p].d < sta[next].d) { break; }
			}
			next--;
			ans += ((l / disPerGas - leftGas) * sta[p].price);
			leftGas = gasCap - ((sta[next].d - sta[p].d) / disPerGas);
		}
		else {//中途停
			next = minSta;
			double l = sta[next].d - sta[p].d;
			ans += (((l / disPerGas) - leftGas) * sta[p].price);
			leftGas = 0;
		}
		p = next;
	}
	printf("%.2f", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值