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); }
相关文章推荐
- 【PAT 1033】To Fill or Not to Fill
- 1033. To Fill or Not to Fill (25)解题报告
- pat甲级-1033-To Fill or Not to Fill
- Pat(Advanced Level)Practice--1033(To Fill or Not to Fill)
- pat 1033 To Fill or Not to Fill(值得重点回顾)
- PAT 1033 To Fill or Not to Fill 贪心
- 1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill (25)加油站
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)【贪心+模拟】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25) -贪心算法
- 1033. To Fill or Not to Fill (25) -贪心算法
- 1033. To Fill or Not to Fill (25)-PAT