HDOJ 4717 三分法

两个点列式展开后发现距离的平方是二次函数, 之中取最大值仍然是个二次函数, 三分法即可

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 111;
const double eps = 1e-6;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;



double x[333], y[333], vx[333], vy[333];
int n;

inline double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double check(double t)
{
    int i, j;
    double mx = 0;
    double tx1, ty1, tx2, ty2;
    for (i = 0; i < n; ++i)
        for (j = i + 1; j < n; ++j)
        {
            tx1 = x[i] + vx[i] * t;
            ty1 = y[i] + vy[i] * t;
            tx2 = x[j] + vx[j] * t;
            ty2 = y[j] + vy[j] * t;
            mx = MAX(mx, dis(tx1, ty1, tx2, ty2));
        }
    return mx;
}


int main()
{
    int T, cas = 1;
    scanf("%d", &T);
    while (T--)
    {
        int i, j, k;
        scanf("%d", &n);
        for (i = 0; i < n; ++i)
        {
            scanf("%lf%lf%lf%lf", &x[i], &y[i], &vx[i], &vy[i]);
        }
        double mid1, mid2, l = 0, r = 1e8;
        double ans;
        while (fabs(r - l) > eps)
        {

            mid1 = (2 * l + r) / 3;
            mid2 = (l + 2 * r) / 3;
            if (check(mid1) < check(mid2)) r = mid2;
            else l = mid1;
        }
        ans = check(mid1);
        printf("Case #%d: %.2f %.2f\n", cas++, mid1, ans);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值