题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717
思路:第一次写三分法,原理和二分法其实是一样的,计算过程两边for,时间复杂度为O(n^2log(n))
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
const double eps = 1e-6;
const int N = 305;
#define max(a,b) ((a)>(b)?(a):(b))
int t, n, i;
struct Point {
double x, y, dx, dy;
void read() {
scanf("%lf%lf%lf%lf", &x, &y, &dx, &dy);
}
} p[N];
double calu(Point a, Point b, double t) {
double dx = a.x + t * a.dx - (b.x + t * b.dx);
double dy = a.y + t * a.dy - (b.y + t * b.dy);
return sqrt(dx * dx + dy * dy);
}
double cal(double t) {
double ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double tmp = calu(p[i], p[j], t);
ans = max(ans, tmp);
}
}
return ans;
}
double solve(double l, double r) {
while (l - r < -eps) {
double mid1 = (l * 2 + r) / 3;
double mid2 = (l + 2 * r) / 3;
if (cal(mid1) - cal(mid2) < -eps)
r = mid2;
else
l = mid1;
}
return l;
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 0; i < n; i++)
p[i].read();
double ans = solve(0, 1e8);
printf("Case #%d: ", ++cas);
if (n == 1) printf("0.00 0.00\n");
else printf("%.2lf %.2lf\n", ans, cal(ans));
}
return 0;
}
HDU 4717 The Moving Points(三分),布布扣,bubuko.com
HDU 4717 The Moving Points(三分)
原文地址:http://blog.csdn.net/accelerator_/article/details/25406453