首页 > 其他 > 详细

HDU 4717 The Moving Points(三分)

时间:2014-05-09 21:11:35      阅读:394      评论:0      收藏:0      [点我收藏+]

题目链接: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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!