#include<stdio.h>
#include<algorithm>
using namespace std;
#define max 510
struct nod
{
double dst;
double price;
}node[max];
int cmp(nod a,nod b)
{
return a.dst<b.dst;
}
int main()
{
double c,d,perd;
int n;
//freopen("in.txt","r",stdin);
scanf("%lf%lf%lf%d",&c,&d,&perd,&n);//dst/gas
for(int i=0;i<n;i++)
scanf("%lf%lf",&node[i].price,&node[i].dst);
sort(node,node+n,cmp);
double vol=0;
double ds=0;
double cost=0;
int index=0;
const double maxl=perd*c;
if(node[0].dst!=0)
{
printf("The maximum travel distance = %.2lf\n",ds);
return 0;
}
while(index<n&&ds<d)
{
double maxn=maxl+ds;
int min=index;
for(int i=index+1;i<n&&maxn>=node[i].dst;i++)
if(node[i].price<=node[min].price)
{
min=i;
break;
}
if(min!=index)//能到达下一个更便宜的站点
{
double tmp=(node[min].dst-node[index].dst)/perd;
if(vol<tmp)
{
cost+=(tmp-vol)*node[index].price;
vol=0;
ds=node[min].dst;
index=min;
}else
{
vol-=tmp;
ds=node[min].dst;
index=min;
}
}else
{
if(index+1<n)//有下一站
{
if(maxn<node[index+1].dst)//下一站不可到达
{
cost+=(c-vol)*node[index].price;
vol=0;
index=n;
ds=maxn;
}else //下一站可以到达
{
if(maxn>=d)//终点可到达
{
double tmp=(d-ds)/perd;
if(tmp>vol)
cost+=(tmp-vol)*node[index].price;
index=n;
ds=d;
}else//终点暂时到达不了 ,下一个站点
{
cost+=(c-vol)*node[index].price;
vol=c-(node[index+1].dst-node[index].dst)/perd;
ds=node[index+1].dst;
index=index+1;
}
}
}else//无下一站
{
if(maxn>=d)//终点可以到达
{
double tmp=(d-ds)/perd;
if(tmp>vol)
cost+=(tmp-vol)*node[index].price;
index=n;
ds=d;
}else
{
cost+=(c-vol)*node[index].price;
ds=maxn;
index=n;
vol=0;
}
}
}
}
if(ds!=d)
printf("The maximum travel distance = %.2lf\n",ds);
else
printf("%.2lf\n",cost);
return 0;
}
PAT1033
最新推荐文章于 2024-03-22 06:13:25 发布