There are N points in total. Every point moves in certain direction and certain speed. We want to know at what time that the largest distance between any two points would be minimum. And also, we require you to calculate that minimum distance. We guarantee that no two points will move in exactly same speed and direction.
Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers X i, Y i, VX i and VY i (-10 6 <= X i, Y i <= 10 6, -10 2 <= VX i , VY i <= 10 2), (X i, Y i) is the position of the i th point, and (VX i , VY i) is its speed with direction. That is to say, after 1 second, this point will move to (X i + VX i , Y i + VY i).
Output
For test case X, output "Case #X: " first, then output two numbers, rounded to 0.01, as the answer of time and distance.
Sample Input
2
2
0 0 1 0
2 0 -1 0
2
0 0 1 0
2 1 -1 0
Sample Output
Case #1: 1.00 0.00
Case #2: 1.00 1.00
思路:距离关于时间的函数,很明显是一个二次函数,先增后减。因此我们可以三分去做。对时间三分,但是要注意精度问题。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define esp 1e-10
using namespace std;
const int maxx=3e2+10;
struct node{
double x;
double y;
double vx;
double vy;
}p[maxx];
int n;
double check(double t)
{
double maxn=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
double tx1=p[i].x+p[i].vx*t;
double ty1=p[i].y+p[i].vy*t;
double tx2=p[j].x+p[j].vx*t;
double ty2=p[j].y+p[j].vy*t;
maxn=max(maxn,sqrt((tx1-tx2)*(tx1-tx2)+(ty1-ty2)*(ty1-ty2)));
}
}
return maxn;
}
double cal()
{
double l=0;
double r=1e10;
while(r-l>=esp)
{
double mid=(l+r)/2;
double midd=(mid+r)/2;
if(check(mid)>check(midd)) l=mid+esp;
else r=midd-esp;
}
return l;
}
int main()
{
int t;
scanf("%d",&t);
int k=0;
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].vx,&p[i].vy);
double x=cal();
printf("Case #%d: %.2lf %.2lf\n",++k,x,check(x));
}
}
努力加油a啊,(o)/~