Straight Shot

topic:

Give the number of conveyor belts n, the initial speed V, the target coordinate X, and in the next n lines, give the left and right coordinates of the conveyor belt l[i], r[i] and the conveyor belt speed vi (positive and negative at the top), ask if you can How long will it take to reach the destination, if so?

 If the robot cannot reach the destination in at most twice the time it would take in the absence of all moving sidewalks (i.e., 2X/v), indicate this.

solution:

Divide the horizontal speed to judge whether it can reach the end point

AC:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=111;
int n,i;
double X,V,dx,dy;
double lim,goal=1e100,tmp,L,R,MID,l[N],r[N],v[N];
double cal(double dy){
    double dx=sqrt(V*V-dy*dy);
    double x=0,y=0;
    for(int i=1;i<=n;i++){
        y+=dy*(l[i]-x);
        y+=(dy+v[i])*(r[i]-l[i]);
        x=r[i];
    }
    y+=dy*(X-x);
    tmp=X/dx;
    return y/dx;
}
int main(){
    scanf("%d%lf%lf",&n,&X,&V);
    for(i=1;i<=n;i++)scanf("%lf%lf%lf",&l[i],&r[i],&v[i]);
    L=-V,R=V;
    for(int _=1000;_;_--){
        MID=(L+R)/2;
        if(cal(MID)<0)L=MID;else R=MID;
    }
    lim = X / V * 2 ;
    L=(L+R)/2;
    if(fabs(cal(L))<1e-8){
        cal(L);
        goal=tmp;
    }
    if(goal>lim+1e-8)puts("Too hard");
    else printf("%.3f",goal);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886774&siteId=291194637