A1033

没考虑到的点:
1、在没有更低油价的加油站时,前往油价尽可能低的加油站
2、当没有距杭距离为0的加油站时特判输出最远距离

//没能解决的问题: 
//1、now_Price,consumeGap_Total没有处理好,只能适用于某些情况. 
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
struct node{
	double singlePrice;
	double hangzhouD;
	double consumeGap;
}station[510];
bool cmp(node a,node b){
	return a.hangzhouD<b.hangzhouD;
}
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	int n;
	double totalC,totalD,aveD,now_Price=0,now_Distance=0,now_GapC=0,consumeGap_Total=0;
	scanf("%lf%lf%lf%d",&totalC,&totalD,&aveD,&n);
	for(int i=0;i<n;i++){                                           //输入 
		scanf("%lf%lf",&station[i].singlePrice,&station[i].hangzhouD);
	}
	sort(station,station+n,cmp);                                    //加油站离杭距离排序 
	if(station[0].hangzhouD!=0){
		printf("The maximum travel distance = 0.00");
		return 0;	
	}
	station[n].hangzhouD=totalD;
	for(int i=0;i<n;i++){                                         //获取各区间耗油量 
		station[i].consumeGap=(station[i+1].hangzhouD-station[i].hangzhouD)/aveD;
	}
	int j=0;
	for(int i=0;i<n;i++){
		if(i==j){
			now_GapC=consumeGap_Total;
			consumeGap_Total+=station[i].consumeGap;
			if(station[i].consumeGap>totalC){
				now_Distance+=totalC*aveD;
				printf("The maximum travel distance = %.2f",now_Distance);
				break;
			}
			for(j=i+1;j<n&&station[j].singlePrice>station[i].singlePrice;j++){
				consumeGap_Total+=station[j].consumeGap;
				if(consumeGap_Total>=totalC){
					consumeGap_Total=totalC;
					break;
				}
			}
			now_GapC=consumeGap_Total-now_GapC;
		}else{
			now_GapC=0;
		}
		now_Price+=now_GapC*station[i].singlePrice;
		now_Distance+=station[i+1].hangzhouD-station[i].hangzhouD;
		consumeGap_Total-=station[i].consumeGap;
	}
	if(now_Distance>=totalD)printf("%.2f",now_Price);
	return 0;
}

对着算法笔记敲了一通:

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=510;
const int INF=1000000000;
struct station{
	double price,dis;                //价格、与起点的距离 
}st[maxn];
bool cmp(station a,station b){
	return a.dis<b.dis; 
}
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	int n;
	double Cmax,D,Davg;
	scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&st[i].price,&st[i].dis);
	}
	st[n].price=0;									  //数组最后面放置终点,价格为0
	st[n].dis=D;									  //终点距离为D
	sort(st,st+n,cmp);
	if(st[0].dis!=0){								  //如果排序后的第一个加油站距离不是0,说明无法前进 
		printf("The maximum travel distance = 0.00\n");
	}else{
		int now=0;									  //当前所处的加油站编号
		//总花费、当前油量及满油行驶距离
		double ans=0,nowTank=0,MAX=Cmax*Davg;
		while(now<n){						 		  //每次循环将选出下一个需要到达的加油站 
			//选出从当前加油站满油能到达范围内的第一个油价低于当前油价的加油站
			//如果没有低于当前油价的加油站,则选择价格最低的那个
			int k=-1;                       		  //最低油价的加油站的编号 
			double priceMin=INF;            		  //最低油价
			for(int i=now+1;i<=n&&st[i].dis-st[now].dis<=MAX;i++){
				if(st[i].price<priceMin){             //如果油价比当前最低油价低 
					priceMin=st[i].price;             //更新最低油价 
					k=i;
					//如果找到第一个油价低于当前油价的加油站,直接中断循环 
					if(priceMin<st[now].price){
						break;
					}
				}
			} 
			if(k==-1)break;							  //满油状态下无法找到加油站,退出循环输出结果
			//下面为能找到可到达的加油站k,计算转移花费
			//need为从now到k需要的油量
			double need=(st[k].dis-st[now].dis)/Davg;
			if(priceMin<st[now].price){               //如果加油站k的油价低于当前油价 
				//只买足够到达加油站k的油
				if(nowTank<need){
					ans+=(need-nowTank)*st[now].price;//补足need
					nowTank=0;                        //到达加油站k后邮箱内油量为0 
				}else{								  //如果当前油量超过need 
					nowTank-=need;					  //直接到达加油站k 
				} 
			}else{									  //如果加油站k的油价高于当前油价 
				ans+=(Cmax-nowTank)*st[now].price;    //将邮箱加满
				//到达加油站k后邮箱内油量为Cmax-need
				nowTank=Cmax-need; 
			}
			now=k; 
		}
		if(now==n){
			printf("%.2f\n",ans);
		}else{
			printf("The maximum travel distance = %.2f\n",st[now].dis+MAX);
		}
	}
	return 0;
}

自己敲了一下:

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=510;
const double INF=1000000000.0;
struct station{
	double price,dis;
}st[maxn];
bool cmp(station a,station b){
	return a.dis<b.dis;
}
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	int n;
	double Cmax,D,Davg;
	scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&st[i].price,&st[i].dis);
	}
	sort(st,st+n,cmp);
	st[n].price=0;
	st[n].dis=D;
	int stNow=0;
	double Dmax=Cmax*Davg,totalPrice=0,nowTank=0;
	if(st[0].dis!=0){
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}
	while(stNow<n){
		int k=-1;
		double price_min=INF;
		for(int i=stNow+1;i<=n&&st[i].dis-st[stNow].dis<=Dmax;i++){
			if(st[i].price<price_min){
				price_min=st[i].price;
				k=i;
				if(st[i].price<st[stNow].price){
					break;
				}
			}
		}
		if(k==-1)break;
		double need=(st[k].dis-st[stNow].dis)/Davg;
		if(st[k].price<st[stNow].price){
			if(nowTank<need){
				totalPrice+=(need-nowTank)*st[stNow].price;
				nowTank=0;
			}else{
				nowTank-=need;
			}
		}else{
			totalPrice+=(Cmax-nowTank)*st[stNow].price;
			nowTank=Cmax-need;
		}
		stNow=k;
	}
	if(stNow==n){
		printf("%.2f\n",totalPrice);
	}else{
		printf("The maximum travel distance = %.2f\n",Dmax+st[stNow].dis);
	}
	return 0;
}

第二天写,两处地方没考虑到:

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=510;
const double INF=1000000000; 
struct station{
	double dis,price;
}stu[maxn];
bool cmp(station a,station b){
	return a.dis<b.dis;
}
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	double Cmax,D,Davg,total=0,Cbegin=0;
	int n;
	scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&stu[i].price,&stu[i].dis);
	}
	sort(stu,stu+n,cmp);
	if(stu[0].dis!=0){                                                 //当第一个加油站距杭不是0时,需特判,遗漏了 
		printf("The maximum travel distance = 0.00\n");
		return 0;
	}
	stu[n].dis=D;
	stu[n].price=0;
	int now=0;
	double MAX=Cmax*Davg;
	while(now<n){
		int k=-1;
		double priceMin=INF;
		for(int i=now+1;i<=n&&stu[i].dis-stu[now].dis<=MAX;i++){
			if(stu[i].price<priceMin){
				priceMin=stu[i].price;
				k=i;
				if(priceMin<stu[now].price){
					break;
				}
			}
		}
		if(k==-1)break;
		double need=(stu[k].dis-stu[now].dis)/Davg;
		if(priceMin<stu[now].price){                                          
			if(Cbegin<need){									//需判断need,Cbegin哪个更大,开始时遗漏了 
				total+=(need-Cbegin)*stu[now].price;
				Cbegin=0;
			}else{
				Cbegin-=need;
			}
		}else{
			total+=(Cmax-Cbegin)*stu[now].price;
			Cbegin=Cmax-need;
		}
		now=k;
	}
	if(now!=n){
		printf("The maximum travel distance = %.2f\n",MAX+stu[now].dis);
	}else{
		printf("%.2f\n",total);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值