The Moving Points
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1874 Accepted Submission(s): 768
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.00 Case #2: 1.00 1.00
Source
Recommend
zhuyuanchen520
题意:告诉你n个点的坐标和运动方向,问哪一个时刻,所有点的最大距离最小。
解题思路:分析对于两个点,他们之间的距离随时间一定是一个二次函数,距离一定是先减小,再增大。用三分逼近这个最小值。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const double exps=1e-8;
int n;
struct node
{
double x,y,vx,vy;
}a[500];
double dis(node a,node b,double t)
{
return sqrt((a.x+a.vx*t-b.x-b.vx*t)*(a.x+a.vx*t-b.x-b.vx*t)+(a.y+a.vy*t-b.y-b.vy*t)*(a.y+a.vy*t-b.y-b.vy*t));
}
double check(double x)
{
double ma=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
ma=max(ma,dis(a[i],a[j],x));
return ma;
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++) scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i].vx,&a[i].vy);
double l=0,r=10000000;
while(r-l>=exps)
{
double mid=(l+r)/2;
double midd=(mid+r)/2;
if(check(mid)<check(midd)) r=midd;
else l=mid;
}
printf("Case #%d: %.2lf %.2lf\n",cas++,r,check(r));
}
return 0;
}