The Moving Points
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1318 Accepted Submission(s): 538
Total Submission(s): 1318 Accepted Submission(s): 538
Problem Description
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 ).
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
Source
大致题意:有n个点,给出每个点的初始坐标x,y和在x,y方向上的运动速度vx,vy。求时间t使得n个点中任意两点之间的距离最大值的最小,输出此时的时间t和任意两点间的最大距离的最小值。
分析:由于,各点的初始坐标和各方向运动速度已知,则可求出时间t后各点的坐标即(x+vx*t,y+vy*t)。
一般来讲,任意两动点之间的距离总是先小后大,而且题意也保证不会出现运动方向和速度均相同的两点,故任意两点之间的最大距离也是一个凹函数,可用三分求最小值。
要注意精度问题,要保证精度在1e-5以上。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-14;
const int maxn=303;
int n;
struct node{
double x,y,vx,vy;
}e[maxn*maxn];
double cross(node a,node b,double t)
{
return (a.x+a.vx*t-b.x-b.vx*t)*(a.x+a.vx*t-b.x-b.vx*t)+(a.y+a.vy*t-b.y-b.vy*t)*(a.y+a.vy*t-b.y-b.vy*t);
}
double find(double t)
{
double ans=0;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
ans=max(ans,cross(e[i],e[j],t));
return ans;
}
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
int i,j,k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&e[i].x,&e[i].y,&e[i].vx,&e[i].vy);
double l,r,m1,m2;
l=0,r=1e8;
for(i=0;i<100;i++)
{
m1=l+(r-l)/3;
m2=r-(r-l)/3;
if(find(m1)<find(m2)) r=m2;
else l=m1;
}
printf("Case #%d: %.2lf %.2lf\n",++tt,l,sqrt(find(l)));
}
return 0;
}
/*
每组点和速度,根据变量t,横纵坐标就是关于t的以为函数。
求两点距离的平方就是下凸函数。fi(x)=ai*t*t+bi*t+ci;
而求n*(n-1)/2个距离的最大值,s(x)=max{fi(x)},也是下凸函数。
因而可以用三分法求得结果。
*/