hdu 4717(三分求极值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717

思路:三分时间求极小值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAX_N = (300 + 30);
const double eps = 1e-5;

struct Point {
    double x, y;
    double vx, vy;
} point[MAX_N];

int N;
double ans;

double getDist(int i, int j, double t_time)
{
    double x1 = (point[i].x + t_time * point[i].vx);
    double y1 = (point[i].y + t_time * point[i].vy);
    double x2 = (point[j].x + t_time * point[j].vx);
    double y2 = (point[j].y + t_time * point[j].vy);

    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double check(double t_time)
{
    double res = 0.0;
    for (int i = 1; i <= N; ++i) {
        for (int j = i + 1; j <= N; ++j) {
            res = max(res, getDist(i, j, t_time));
        }
    }
    ans = min(ans, res);

    return res;
}

int main()
{
    int Cas, t = 1;
    scanf("%d", &Cas);
    while (Cas--) {
        scanf("%d", &N);
        for (int i = 1; i <= N; ++i) {
            scanf("%lf %lf %lf %lf", &point[i].x, &point[i].y, &point[i].vx, &point[i].vy);
        }

        if (N == 1) {
            printf("%.2f %.2f\n", 0, 0);
            continue;
        }

        ans  = 1e18;

        double low = 0.0, high = 1e8, mid, mmid;
        while (low + eps < high) {
            mid = (low + high) / 2.0;
            mmid = (mid + high) / 2.0;
            if (check(mid) <= check(mmid)) {
                high = mmid;
            } else
                low = mid;
        }

        printf("Case #%d: %.2f %.2f\n", t++, low, ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值