gym/226036-F. Two Points【三分查找】

F. Two Points

There are two points (x1, y1) and (x2, y2) on the plane. They move with the velocities (vx1, vy1) and (vx2, vy2). Find the minimal distance between them ever in future.

Input

The first line contains four space-separated integers x1, y1, x2, y2 ( - 104 ≤ x1,  y1,  x2,  y2 ≤ 104) — the coordinates of the points.
The second line contains four space-separated integers vx1, vy1, vx2, vy2 ( - 104 ≤ vx1,  vy1,  vx2,  vy2 ≤ 104) — the velocities of the points.

Output

Output a real number d — the minimal distance between the points. Absolute or relative error of the answer should be less than 10 - 6.

Examples

Input

1 1 2 2
0 0 -1 0

Output

1.000000000000000

Input

1 1 2 2
0 0 1 0

Output

1.414213562373095


题意:

输入两个点坐标和速度向量
求这两个点能到的最近距离
输入

x 1 , y 1 , x 2 , y 2 v x 1 , v y 1 , v x 2 , v y 2


题解

求出距离表达式

d = ( x 1 x 2 + t × ( v x 1 v x 2 ) ) 2 + ( y 1 y 2 + t × ( v y 1 v y 2 ) ) 2

[ 0 , 10 5 ] 三分 t d 的最小值即可
注意:在三分t的时候精度要高,答案要求为 10 6 但这是距离的精度,时间精度应该更大,这个题需要时间精度高于 10 10
也可以不管精度,直接跑一百遍

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double X1, Y1, x2, y2, vx1, vy1, vx2, vy2;
double pow2(double x) { return x * x; }
double check(double t)
{
    return  pow2(X1 - x2 + t * (vx1 - vx2)) + pow2(Y1 - y2 + t * (vy1 - vy2));
}
int main()
{
    scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &X1, &Y1, &x2, &y2, &vx1, &vy1, &vx2, &vy2);
    double L, R;
    L = 0;
    R = 1e6;
    int tot = 100;
    while (tot--)
    {
        double mid1 = (L + L + R) / 3;
        double mid2 = (R + R + L) / 3;
        double ans1 = check(mid1);
        double ans2 = check(mid2);
        if (ans1 <= ans2)R = mid2;
        else L = mid1;
    }
    double ans = sqrt(check(L));
    printf("%.9f\n", ans);
}

猜你喜欢

转载自blog.csdn.net/jinmingyi1998/article/details/81149829
two
今日推荐