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
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you:
4826
4825
4824
4823
4822
题目大意:给出N个点的坐标 1秒后变成(x+vx,y+vy)求出多少秒后两两最大距离的最小,并求出这个距离。
思路:它的函数为二次函数求极值用三分,将长度三等分,类比于二分,可参考
http://blog.csdn.net/acdreamers/article/details/9989197。
代码:
import java.text.DecimalFormat;
import java.util.*;
public class Main{
static int n;
static int a[]=new int[300];
static int b[]=new int[300];
static int c[]=new int[300];
static int d[]=new int[300];
static double eps=1e-8;
static double cal(double x,int i,int j){
double ans=Math.pow((a[i]+x*c[i]-a[j]-x*c[j]),2)+Math.pow((b[i]+d[i]*x-b[j]-x*d[j]), 2);
return ans;
}
import java.util.*;
public class Main{
static int n;
static int a[]=new int[300];
static int b[]=new int[300];
static int c[]=new int[300];
static int d[]=new int[300];
static double eps=1e-8;
static double cal(double x,int i,int j){
double ans=Math.pow((a[i]+x*c[i]-a[j]-x*c[j]),2)+Math.pow((b[i]+d[i]*x-b[j]-x*d[j]), 2);
return ans;
}
static double find(double t){
double ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
ans=Math.max(ans, cal(t,i,j));
}
}
return ans;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
DecimalFormat df = new DecimalFormat(".00");
int t=scan.nextInt();
int k=0;
while(t-->0){
k++;
System.out.print("Case #"+k+": ");
n=scan.nextInt();
for(int i=0;i<n;i++){
a[i]=scan.nextInt();
b[i]=scan.nextInt();
c[i]=scan.nextInt();
d[i]=scan.nextInt();
}
double l=0,r=1e8;
double tmp,m1,m2;
while(l+eps<r){
tmp=(r-l)/3.0;
m1=l+tmp;
m2=r-tmp;
if(find(m1)+eps<find(m2)){
r=m2;
}
else
l=m1;
}
System.out.print(String.format("%.2f", l)+" ");
System.out.println(String.format("%.2f", Math.sqrt(find(l))));
}
}
}
double ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
ans=Math.max(ans, cal(t,i,j));
}
}
return ans;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
DecimalFormat df = new DecimalFormat(".00");
int t=scan.nextInt();
int k=0;
while(t-->0){
k++;
System.out.print("Case #"+k+": ");
n=scan.nextInt();
for(int i=0;i<n;i++){
a[i]=scan.nextInt();
b[i]=scan.nextInt();
c[i]=scan.nextInt();
d[i]=scan.nextInt();
}
double l=0,r=1e8;
double tmp,m1,m2;
while(l+eps<r){
tmp=(r-l)/3.0;
m1=l+tmp;
m2=r-tmp;
if(find(m1)+eps<find(m2)){
r=m2;
}
else
l=m1;
}
System.out.print(String.format("%.2f", l)+" ");
System.out.println(String.format("%.2f", Math.sqrt(find(l))));
}
}
}