1033__部分正确

怀疑是精度问题,只得4分

哎以后再调吧

ref:

http://blog.csdn.net/matrix5467/article/details/8640728

#include <stdio.h>
#include <algorithm>
#include <math.h>
#define NUM 505
#define INF 9999999

typedef struct{
	double price;
	double dist;
}station;

station s[NUM];

int cmp(const void * a, const void * b){
	station sa = *(station *)a;
	station sb = *(station *)b;
	return (sa.dist-sb.dist);
}

int main(){
	freopen("in.txt","r",stdin);
	
	int  n;//加油站个数
	double cmax, dest ;//邮箱容量,全程、每单位油跑的里程
	int davg;
	scanf("%lf%lf%d%d", &cmax, &dest, &davg, &n);

	for(int i = 0; i < n; i++){
		station st;
		scanf("%lf %lf",&st.price, &st.dist);
		s[i] = st;
	}

	qsort(s,n,sizeof(station),cmp);//按距离排序加油站


	
	double curTank = 0;

	double maxRun = cmax * davg;//装满油能跑得最远距离
	double res = 0;//油钱
	


	int i = 0;
	double lastPrice = 0;//上一次加油时的价钱

	while(i < n){
		//test
		//printf("when at %d, tank is %lf\n",i, curTank);

		if(i == n - 1){//走到最后一个加油站了,
			double toEndneed = (dest - s[i].dist)/davg;//剩下距离需要的油量

			if(cmax < toEndneed){//到不了终点
				double fareast = s[i].dist + cmax * davg;
				printf("The maximum travel distance = %.2lf",fareast);

			}else if(curTank >= toEndneed){//油足够 甚至 还有剩余
				res -= (curTank - toEndneed) * lastPrice;
				printf("%.2lf",res);

			}else if(curTank < toEndneed){
				res += (toEndneed - curTank) * s[i].price;
				printf("%.2lf",res);
			}
			break;
		}

		//找到maxRun范围内最便宜的油站B
		int B = i;
		double sumDes = 0, minPri = s[i].price;
		for(int j = i+1; sumDes < maxRun && j < n; j++){	

			

			sumDes = s[j].dist-s[i].dist;

			//test
			//printf("j = %d minPri = %lf price = %lf sumDes=%lf\n",j, minPri,s[j].price,sumDes);

			if(sumDes < maxRun && minPri > s[j].price){//能跑到j,并且j站便宜
				minPri = s[j].price;
				B = j;
			}
		}

		//test
		//printf("when i = %d found B = %d\n",i, B);

		if(B == i){
			//test
			//printf("case 1\n");
			//printf("when at %d, res = %lf\n",i,res);

			res += (cmax-curTank) * s[i].price;//i站最便宜,那就加满油再跑
			lastPrice = s[i].price;

			//求最远能到的下一站
			int k = i+1;
			for(double sumDes = 0; sumDes < maxRun && k < n; k++){
				sumDes = s[k].dist-s[i].dist;
				if(sumDes > maxRun){					
					break;
				}
			}
			//跑到k-1和k之间油干了,或者是跑得过了最后一个加油站(k==n)
			curTank =  cmax - (s[k-1].dist - s[i].dist)/davg;//耗油:从i到k-1
			//int tmp = (curTank)*10;
			//curTank = tmp/10.0;  //精确到小数点后两位
			
			i = k - 1;

			//test
			//printf("add then, res = %lf  \n",res, i);
			
		}else{
			//test
			//printf("case 2222\n");
			//printf("when at %d, res = %lf\n",i,res);

			double toBneeds = (s[B].dist - s[i].dist)/davg;//跑到B需要的油量
			if(curTank >= toBneeds){//现有油足够				
				curTank -= toBneeds;//耗油
				//花钱为0
			}else{
				res += (toBneeds - curTank) * s[i].price;//在i站加够刚好能跑到B的油
				lastPrice = s[i].price;

				curTank = 0;//到B时油干了
			}
			
			i = B;//下一站跑到B

			//test
			//printf("add then, res = %lf  \n",res, i);
		}
		//printf("\n");
	}


	return 0;
}


你可能感兴趣的:(1033__部分正确)