您的位置:首页 > 其它

1033. To Fill or Not to Fill

2014-07-20 17:10 337 查看
看当前站可达范围内的加油站,加足够的油到第一个更便宜的站,或者加满油到紧邻的下一个站

#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: