codeforces 5D Follow Traffic Rules
简单数学题,注意分类讨论即可,把特殊情况考虑清楚。
#include<bits/stdc++.h>
using namespace std;
int main(){
double a,v,l,d,w;
cin>>a>>v>>l>>d>>w;
if(v<=w){
double sp=sqrt(2*a*d);
if(sp<=v){
double t1=sqrt(2*d/a);
double delta=sp*sp-2*a*(d-l);
double t2=(-sp+sqrt(delta))/a;
if(sp+t2*a<=v) {
cout<<fixed<<setprecision(10)<<t1+t2<<endl;
}else {
double tp=(v-sp)/a;
cout<<fixed<<setprecision(10)<<t1+tp+(l-d-sp*tp-a/2*tp*tp)/v<<endl;
}
} else {
double t1=v/a;
double dist=d-a*t1*t1/2;
t1+=dist/v;
cout<<fixed<<setprecision(10)<<t1+(l-d)/v<<endl;
}
} else {
double sp=sqrt(2*a*d);
if(sp<=w){
double t1=sqrt(2*d/a);
double delta=sp*sp-2*a*(d-l);
double t2=(-sp+sqrt(delta))/a;
if(sp+t2*a<=v) {
cout<<fixed<<setprecision(10)<<t1+t2<<endl;
}else {
double tp=(v-sp)/a;
cout<<fixed<<setprecision(10)<<t1+tp+(l-d-sp*tp-a/2*tp*tp)/v<<endl;
}
} else {
double t1=w/a;
double dist=d-a*t1*t1/2;
double tt=sqrt( ( a*t1*t1+dist)/a);
if(tt*a<=v)
t1+=2*(tt-t1);
else {
double nd=v/a;
double dt=a/2*nd*nd;
t1=nd+(nd-t1)+(d-dt-(dt-a/2*t1*t1))/v;
}
double delta=w*w-2*a*(d-l);
double t2=(-w+sqrt(delta))/a;
if(w+t2*a<=v){
cout<<fixed<<setprecision(10)<<t1+t2<<endl;
}else {
double tp=(v-w)/a;
cout<<fixed<<setprecision(10)<<t1+tp+(l-d-w*tp-a/2*tp*tp)/v<<endl;
}
}
}
return 0;
}