HDU 4717 The Moving Points

The Moving Points

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 260    Accepted Submission(s): 104


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).
 

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
2013 ACM/ICPC Asia Regional Online —— Warmup2
 

Recommend
zhuyuanchen520
 
题意:有N个点,点有个运动方向,随着时间不断移动。 问哪个时刻, 点集的最大距离最小。
思路:三分 O(N ^ 2 log L)
三分时刻, 求出最佳时刻。
#include
#include
#include
using namespace std;

const double EP = 0.0001;
const int V = 300 + 5;

struct Point {
    double x, y, Vx, Vy;
};
Point poi[V];
int n, T, s = 1;
double getLine(Point a, Point b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double cal(double time) {
    double sum = 0.0;
    Point p[V];
    for(int i = 0; i < n; ++i) {
        p[i].x = poi[i].x + poi[i].Vx * time;
        p[i].y = poi[i].y + poi[i].Vy * time;
    }
    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            sum = max(sum, getLine(p[i], p[j]));
    return sum;
}
double solve(double l, double r) { //三分
    while(l + EP < r) {
        double m1 = (l * 2 + r) /3.0;
        double m2 = (l + r * 2) /3.0;
        if(cal(m1) < cal(m2)) //极小值
            r = m2;
        else
            l = m1;
    }
    return l;
}
int main() {
    int i, j;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(i = 0; i < n; ++i)
            scanf("%lf%lf%lf%lf", &poi[i].x, &poi[i].y, &poi[i].Vx, &poi[i].Vy);
        printf("Case #%d: ", s++);
        if(n == 1) {
             printf("0.00 0.00\n");
             continue;
        }
        double ti = solve(0, 1e8);
        printf("%.2lf %.2lf\n", ti, cal(ti));
    }
}


你可能感兴趣的:(三分)