1033. To Fill or Not to Fill

来源:互联网 发布:服务器机柜和网络机柜 编辑:程序博客网 时间:2024/05/14 05:30

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

#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
原创粉丝点击