PAT A1033

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080

在加满油的距离下找有没有比当前便宜的,有,就加去第一个比它便宜的地方的油,如果没有,就加满油去剩下的最便宜的站。

注意用一个变量存当前油量。

把终点看成油不要钱的站。
 

#include <cstdio>
#include <algorithm>
using namespace std;
const double INF = 1e9; 
struct station{
	double p;
	double d;
}P[510];
bool cmp(station a, station b){
	if (a.d != b.d)
		return a.d < b.d;
	else
		return a.p < b.p;
}
int main(){
	double Cmax, D, Davg;
	int N;
	scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
	for(int i = 0; i < N; i++)
		scanf("%lf%lf", &P[i].p, &P[i].d);
	P[N].d = D;
	P[N].p = 0;
	sort(P, P+N, cmp);
	int now = 0;
	if(P[now].d){  //这个很坑
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	double nowtank = 0, money = 0, maxx = Cmax * Davg, need;
	while(now != N){
		int min_i = -1;
		double min_p = INF;
		for(int i = now + 1; i <= N && P[i].d <= P[now].d + maxx; i++){
			if(P[i].p < P[now].p){
				min_i = i;
				need = (P[i].d - P[now].d) / Davg;
				if(need > nowtank){
					money += (need - nowtank) * P[now].p;
					nowtank = 0;
				}
				else
					nowtank -= need;
				now = i;
				break;
			}
			if(min_p > P[i].p){
				min_i = i;
				min_p = P[i].p;
			}
		}
		if(min_i == -1){
			money += (Cmax - nowtank) * P[now].p;
			break;
		}
		else if(min_i != now){
			money += (Cmax - nowtank) * P[now].p;
			need = (P[min_i].d - P[now].d) / Davg;
			nowtank = Cmax - need;
			now = min_i;
		}
	}

	if(now == N)
		printf("%.2f", money);
	else
		printf("The maximum travel distance = %.2lf", P[now].d + maxx);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值