HDU - 4717 The Moving Points
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<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include <cctype>
#include <stack>
#define N 200100
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;
const double eps=0.000001;
const int M=310;
int n;
struct point{
double x,y,vx,vy;
}po[M];
double dis(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cal(double t){
double ans=0;
double num;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
num=dis(po[i].x+t*po[i].vx,po[i].y+t*po[i].vy,po[j].x+t*po[j].vx,po[j].y+t*po[j].vy);
ans=max(num,ans);
}
}
return ans;
}
double solve(double l,double r){
double mid1,mid2;
while(r-l>=eps){
mid1=(l*2+r)/3;
mid2=(l+r*2)/3;
if(cal(mid2)-cal(mid1)>eps){
r=mid2;
}
else{
l=mid1;
}
}
return l;
}
int main(){
int t,cas,i;
double ans;
cas=0;
scanf("%d",&t);
while(t--){
cas++;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&po[i].x,&po[i].y,&po[i].vx,&po[i].vy);
}
ans=solve(0,1e8);
printf("Case #%d: ",cas);
if(n==1){
printf("0.00 0.00\n");
}
else{
printf("%.2lf %.2lf\n",ans,cal(ans));
}
}
return 0;
}