The Moving Points
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1904 Accepted Submission(s): 777
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.00Case #2: 1.00 1.00
题意:给出n个点的坐标和运动速度(包括方向)。求一个时刻t使得该时刻时任意两点距离最大值最小。
思路:每两个点之间的距离随时间的变化是一个开口向上的抛物线。把所有的抛物线画出来。然后每个时刻取最大值。发现这个最大值是单峰函数。
但是还是没有想清楚这个原理是什么,网上感觉讲的也不太详细,还是以后慢慢想
#include<bits/stdc++.h> using namespace std; #define EPS 1e-7 #define clr(x) memset(x, 0, sizeof(x)) struct node { double x, y, vx, vy; }N[305]; int n; double dist(double x1, double x2, double y1, double y2) { return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); } double getsum(double t) { double sum = 0; for(int i = 0; i < n; i++) for(int j = i + 1; j < n ; j++) { sum = max(sum,dist(N[i].x + N[i].vx*t, N[j].x + N[j].vx*t, N[i].y + N[i].vy *t, N[j].y + N[j].vy*t)); } return sum; } int main() { int t, cnt = 0; scanf("%d", &t); while(t--) { scanf("%d", &n); clr(N); for(int i = 0; i < n; i++) scanf("%lf %lf %lf %lf", &N[i].x, &N[i].y, &N[i].vx, &N[i].vy); double l = 0, r = 10000000; while(r - l >= EPS) { double mid= (l + r) / 2.0; double mmid = (mid + r) / 2.0; if(getsum(mid) - getsum(mmid) > EPS) l = mid; else r = mmid; } printf("Case #%d: %.2lf %.2lf\n", ++cnt, l, getsum(l)); } return 0; }