PAT-1033 To Fill or Not to Fill

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eric4784510/article/details/82026116

1033 To Fill or Not to Fill(25 分)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

    贪心算法,不管路程的前后顺序,按价格从小到大排序,每次找最便宜的站加油,尽量多加。记录之前加的油已走过的路程每次经过加油站时剩余的油量,以计算本次可以加多少油。因为数据不大就开数组记录了,debug时方便查看状态。

    注意nowcoder上C++11编译器中 addlen*1.0f/davg;   必须写成    addlen*1.0f/davg*1.0;才没有精度损失,PAT没有这个问题

#include<stdio.h>
#include<algorithm>
using namespace std;

typedef struct gas{
	int dis;
	double price;
}gas;
gas g[505];

bool way[30005]={0};//已走的路
int tank[30005]={0};//油箱剩的油可跑的距离
bool cmp(gas g1,gas g2){
	return g1.price<g2.price;
}
int main(){
	int cmax,d,davg,n;
	scanf("%d %d %d %d",&cmax,&d,&davg,&n);
	for(int i=0;i<n;i++){
		scanf("%lf %d",&g[i].price,&g[i].dis);
	}
	int fulldis=cmax*davg;//加满油能跑的距离
	sort(g,g+n,cmp);//按价格排序
	double amount=0.0;
	for(int i=0;i<n;i++){
		int maxdis=g[i].dis+fulldis;
		int tmp=min(maxdis,d);
		while(way[tmp]&&tmp>g[i].dis)
			tmp--;

		int addlen=tmp-g[i].dis-tank[g[i].dis];//该站加的油能跑的距离
		if(addlen<=0)//该站不需要加油,因为它的前后有更便宜的
			continue;
		double addgas=addlen*1.0f/davg*1.0;
		int t=addlen;

		if(tank[g[i].dis]==0) tank[g[i].dis]=t--;
		else tank[g[i].dis]+=t;
		for(int j=g[i].dis+1;j<=tmp;j++){
			way[j]=true;//表示这公里走过了
			if(tank[j]==0)tank[j]=t--;
			else tank[j]+=t;//更新油量(用int距离表示避免精度损失)
		}
		amount+=g[i].price*addgas;
	}
	for(int i=1;i<=d;i++){
		if(way[i]==false){
			printf("The maximum travel distance = %.2f",(i-1)*1.0);
			return 0;
		}
	}
	printf("%.2f",amount);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/eric4784510/article/details/82026116
今日推荐