Straight Shot 二分水平分速度
题目:
给出传送带个数n,初速度V,目标坐标X,以后的n行里,分别给出传送带左右坐标l[i],r[i]以及传送带速度vi(上正下负),问能否到达目的地,如果能,时间是多少?
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.
解法:
二分水平速度,判断能否到达终点
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); }