首页 > 代码库 > HDU 4717 The Moving Points(三分)
HDU 4717 The Moving Points(三分)
题目链接: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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。