题目:
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个点,每个点都有自己的起始坐标还有自己在x轴y轴的速度,问彼此之间最小的距离是多少,输出时间and距离(控制在0.01),因为两个点之间的距离并不是线性的关系,但是在求解最小值的问题上,是可以去考虑三分的,距离也是一种函数关系(关于t的函数关系)。
ac代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=500;
struct node{
double x;
double y;
double vx;
double vy;
}p[maxn];
int T,n;
double dis(node a,node b,double t)
{
double x1=a.x+a.vx*t;
double x2=b.x+b.vx*t;
double y1=a.y+a.vy*t;
double y2=b.y+b.vy*t;
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
double cal(double t)
{
double res=0;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
{
res=max(res,dis(p[i],p[j],t));
}
return res;
}
int main()
{
scanf("%d",&T);
int kase=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);
if(n==1)
{//特判的情况
printf("Case #%d: 0.00 0.00\n",++kase);
continue;
}
double l=0,r=9999.9;
for(int kk=0;kk<50;kk++)
{//三分的循环,求解最小的点
double mid2=r-(r-l)/3;
double mid1=l+(r-l)/3;
if(cal(mid1)>cal(mid2))
l=mid1;
else
r=mid2;
}
printf("Case #%d: %.2lf %.2lf\n",++kase,l,sqrt(cal(l)));
}
}