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 Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).
 

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
 
题意:有N个点,点有个运动方向,随着时间不断移动。 问哪个时刻, 点集的最大距离最小。
思路:三分 O(N ^ 2 log L)
三分时刻, 求出最佳时刻。
#include<cstdio>
#include<cmath>
#include<algorithm>
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));
    }
}


展开阅读全文

没有更多推荐了,返回首页