1033. To Fill or Not to Fill

看当前站可达范围内的加油站,加足够的油到第一个更便宜的站,或者加满油到紧邻的下一个站

#include<algorithm>
#include<iostream>
using namespace std;
int cmax,dist,davg,n;
double cost,leftoil,maxlen;
struct node{int d; double p;
  node(double a,int b):p(a),d(b){}
  bool operator<(const node&x)const{return d<x.d;}
};
vector<node>st;
int nextst(int k){
  int maxlen=cmax*davg,next=-1;
  for(int i=k+1,mmin=1<<30;i<=n&&st[i].d<=st[k].d+maxlen;++i)
    if(st[i].p<st[k].p)return i;
  if(st[k+1].d<=st[k].d+maxlen) return k+1;
  return -1;
}
int main(){
  cin>>cmax>>dist>>davg>>n;
  for(int i=0;i<n;++i){
    double a,b;  cin>>a>>b;
    if(b<dist) st.emplace_back(a,b);
  }
  sort(st.begin(),st.end());
  st.emplace_back(0.0,dist);
  if(st[0].d) {maxlen=0.0003;goto hehe;}
  for(int next,i=0;i<n;i=next){
    next=nextst(i);
    if(next==-1) {
      maxlen=st[i].d+cmax*davg;
      break;
    }else{
      double needoil=double(st[next].d-st[i].d)/davg;
      if(st[next].p<st[i].p){
	cost+=max((needoil-leftoil)*st[i].p,0.0);
	leftoil=max(leftoil-needoil,0.0);
      }else{
	cost+=(cmax-leftoil)*st[i].p;
	leftoil=cmax-needoil;
      }//else
    }//else
  }//for
 hehe:
  if(maxlen>0.0002)printf("The maximum travel distance = %.2lf",maxlen);
  else printf("%.2lf",cost);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值