分析:贪心算法;
从起点选择 最近的比起点小的;如果没有选择范围内最小的;其次看距离终点是否在范围内;
过程:调试了很多次,最后只是两个浮点数的问题;
下面是ac 代码;
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
struct zhandian{
double gas;
float dis;
}sta[501];
int n;
int carry,maxd;
int dvag;
bool cmp(const zhandian &a,const zhandian &b)
{
return a.dis<b.dis;
}
int findmin(int chushi,int maxlength)
{
int i=chushi+1;
int next=-1;
float maxgas=1<<30;
if(maxd-sta[chushi].dis<=maxlength)
{
while(sta[i].dis-sta[chushi].dis<=maxlength&&i<n)
{
if(sta[i].gas<=sta[chushi].gas)
{
next=i;break;
}
i++;
}
}
else
while(sta[i].dis-sta[chushi].dis<=maxlength&&i<n)
{
if(sta[i].gas<=sta[chushi].gas)
{
next=i;break;
}
if(sta[i].gas<=maxgas)
{
next=i;
maxgas=sta[i].gas;
}
i++;
}
if(next!=-1)
return next;
else return -1;
}
int main()
{
int tankload=0;
float maxlength;
while(scanf("%d%d%d%d",&carry,&maxd,&dvag,&n)!=EOF)
{
for(int i=0;i<n;i++)
{
cin>>sta[i].gas;
cin>>sta[i].dis;
}
sort(sta,sta+n,cmp);
maxlength=carry*dvag;
int i=0;
float length=0;
float count=0;
int flag=0;
int chushi=0;
float youxiang=0;
if(sta[0].dis!=0)
{
printf("The maximum travel distance = 0.00\n");continue;
}
while(1)
{
int kk=maxd-sta[chushi].dis;
if(kk<=maxlength)
{
int k=findmin(chushi,maxlength);
if(k==-1)
{
length=maxd;
count+=(kk-youxiang*dvag)*sta[chushi].gas/dvag;break;
}
else
{
float kount=sta[k].dis-sta[chushi].dis-youxiang*dvag;
if(kount>=0)
{
count+=kount*sta[chushi].gas/dvag;
youxiang=0;}
else {
youxiang=-kount/dvag;
}
length+=sta[k].dis-sta[chushi].dis;
chushi=k;
}
}
else
{
int k=findmin(chushi,maxlength);
if(k==-1)
{
length+=maxlength;flag=1;break;
}
else{
if(sta[k].gas<=sta[chushi].gas)
{
float kount=sta[k].dis-sta[chushi].dis-youxiang*dvag;
if(kount>=0)
{
count+=kount*sta[chushi].gas/dvag;
youxiang=0;}
else {
youxiang=-1.0*kount/dvag;
}
}
else {
count+=(carry-youxiang)*sta[chushi].gas;
youxiang=carry-(sta[k].dis-sta[chushi].dis)/dvag;
}
length+=sta[k].dis-sta[chushi].dis;
chushi=k;
}
}
}
if(flag==0) printf("%.2f\n",count);
else printf("The maximum travel distance = %.2f\n",length);
}
return 0;
}