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
题意:
输入两个点坐标和速度向量
求这两个点能到的最近距离
输入
题解
求出距离表达式
在[0,105][0,105]三分tt求的最小值即可
注意:在三分t的时候精度要高,答案要求为10−610−6但这是距离的精度,时间精度应该更大,这个题需要时间精度高于10−1010−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);
}

本篇介绍了一道关于两点在平面上运动并求解它们未来可能达到的最小距离的问题。输入包含两点的初始坐标及速度向量,通过计算距离公式得出最小距离。

1801

被折叠的 条评论
为什么被折叠?



