一开始完全没有思路诶,看了网上大神的代码写的,大神好厉害!
最大距离是一个开口向上的抛弧线,我们求的就是其中的最低点,而抛物线则用三分法去逼近,每一时刻暴力全部的线段的长度,求得最长线段。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
#define MIN 1e-6
struct node{
double x;
double y;
}a[310],s[310];
int N;
double cal(double x);
void solve();
int main(){
int T;
int t = 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,&s[i].x,&s[i].y);
}
printf("Case #%d: ",t++);
solve();
}
return 0;
}
double cal(double x){
double m = 0;
for(int i=0;i<N;i++){
double px1 = a[i].x + s[i].x*x;
double py1 = a[i].y + s[i].y*x;
for(int j=i+1;j<N;j++){
double px2 = a[j].x + s[j].x*x;
double py2 = a[j].y + s[j].y*x;
m = max(m,sqrt((px1-px2)*(px1-px2)+(py1-py2)*(py1-py2)));
}
}
return m;
}
void solve(){
double l = 0;
double r = 0xffffff;
while(r-l>MIN){
double tm = (r-l)/3;
double tl = l+tm;
double tr = r-tm;
if(cal(tl)>cal(tr)){
l = tl;
}
else{
r = tr;
}
}
printf("%.2lf %.2lf\n",l,cal(l));
}