1. 程式人生 > >【三分入門】HDU - 4717 Q - The Moving Points

【三分入門】HDU - 4717 Q - The Moving Points

Q - The Moving Points  HDU - 4717

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 thpoint, 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

給你n個點的座標和橫縱座標每秒變換的值,問你哪個時間這n個點中的最大距離最小值

因為只有300個點,又知道每兩個點的距離肯定是成先減後增的拋物線,所以我們三分頂點

每次都n^2找最大值,如果比原來求得的更小就更新時間和答案

先減後增的拋物線
int SanFen(int l,int r) //找凸點  
{  
    while(l < r-1)  
    {  
        int mid  = (l+r)/2;  
        int mmid = (mid+r)/2;  
        if( f(mid) > f(mmid) )  
            l = mid;  
        else  
            r = mmid;  
    }  
    return f(l) > f(r) ? l : r;  
}  
先增後減的拋物線
int SanFen(int l,int r) //找凸點  
{  
    while(l < r-1)  
    {  
        int mid  = (l+r)/2;  
        int mmid = (mid+r)/2;  
        if( f(mid) > f(mmid) )  
            r = mmid;  
        else  
            l = mid;  
    }  
    return f(l) > f(r) ? l : r;  
}  

 

#include <bits/stdc++.h>
using namespace std;
#define eps 1e-4
int n;
struct node
{
    double x,y,vx,vy;
}p[350];

double cal(int a,int b,double t)
{
    double x1=p[a].x+t*p[a].vx,x2=p[b].x+t*p[b].vx;
    double y1=p[a].y+t*p[a].vy,y2=p[b].y+t*p[b].vy;
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double check(double t)
{
    double ans=0.0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            ans=max(ans,cal(i,j,t));
        }
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    int cas=0;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].vx,&p[i].vy);
        }
        double l=0.0,r=100000000000.0,ans_time=0.0,ans_path=10000000000000;
        while(l+eps<=r)
        {
            double mid=(l+r)/2;
            double midd=(mid+r)/2;
            if(check(mid)<=check(midd)) r=midd;
            else l=mid;
            if(ans_path>check(l))
            {
                ans_time=l;
                ans_path=check(l);
            }
        }
        printf("Case #%d: %.2f %.2f\n",++cas,ans_time,ans_path);
    }
    return 0;
}