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
", 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
", t++, low, ans);
    }
    return 0;
}





原文地址:https://www.cnblogs.com/wally/p/4477048.html