1033. To Fill or Not to Fill (25)

Posted zxzmnh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1033. To Fill or Not to Fill (25)相关的知识,希望对你有一定的参考价值。

#include<stdio.h>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int C,D,Davg,N;
double totDis = 0.0;
double totCost = 0.0;
double longDis = 0.0;
double leftCap = 0.0;
typedef struct NODE{
	double price;
	int dis;
}Node;
bool cmp(Node &a,Node &b){
	return a.dis<b.dis;
}
int main()
{
	cin>>C>>D>>Davg>>N;
	vector<Node>station(N+1);
	for(int i=0;i<N;i++){
		cin>>station[i].price>>station[i].dis;
	}
	station[N].price = 0;
	station[N].dis = D;
	sort(station.begin(),station.end(),cmp);
	longDis = C * Davg;
	int index = 0;
	if(station[0].dis != 0 ) {
			printf("The maximum travel distance = 0.00");
			return 0;
	}
	while(index <N){
		double minPri = station[index+1].price;
		double nowDis = station[index].dis ;
		double nexIndex;
		if(station[index + 1].dis - station[index].dis>longDis){
			printf("The maximum travel distance = %.2f",nowDis+longDis);
			return 0;
		}
		for(int j = index+1 ;j<=N && (station[j].dis - nowDis)<=longDis;j++){		
			if(station[j].price >= station[index].price){
				if(station[j].price <= minPri ){
					nexIndex = j;
					minPri  = station[j].price;
				}				
			}
			else{
				nexIndex = j;
				minPri = station[j].price;
				break;
			}
		}
		if(minPri >station[index].price){
			totCost += (C-leftCap) * station[index].price;
			leftCap = C - (station[nexIndex].dis - nowDis)/Davg;
		}
		else{
			double tmp  = (station[nexIndex].dis - nowDis)/Davg;
			if(tmp > leftCap){
				totCost +=(tmp - leftCap) * station[index].price;
				leftCap = tmp;
			}
			leftCap -= tmp;
		}
		totDis = station[nexIndex].dis;
		index  = nexIndex;
	}
	printf("%.2f",totCost);
	return 0;
 } 

  题意;从起点经过多个加油站到达终点,求最小花费,不能到达,求最远距离;

  总结:

      贪心策略,找能到达的首个油价比当前油价低的加油站,如果都没有,找能到达的最便宜的;

     初始邮箱为空,如果起点没有加油站,那么哪都去不了

以上是关于1033. To Fill or Not to Fill (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1033. To Fill or Not to Fill

1033 To Fill or Not to Fill (25 分)

1033. To Fill or Not to Fill (25)

1033 to fill or not to fill

PAT 1033 To Fill or Not to Fill[dp]

1033. To Fill or Not to Fill (25)