HDU-4717 The Moving Points(凸函数求极值)

The Moving Points

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2122    Accepted Submission(s): 884


Problem Description
There are N points in total. Every point moves in certain direction and certain speed. We want to know at what time that the largest distance between any two points would be minimum. And also, we require you to calculate that minimum distance. We guarantee that no two points will move in exactly same speed and direction.
 

 

Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers X i, Y i, VX i and VY i (-10 6 <= X i, Y i <= 10 6, -10 2 <= VX i , VY i <= 10 2), (X i, Y i) is the position of the i th point, and (VX i , VY i) is its speed with direction. That is to say, after 1 second, this point will move to (X i + VX i , Y i + VY i).
 
Output
For test case X, output "Case #X: " first, then output two numbers, rounded to 0.01, as the answer of time and distance.
 
Sample Input
2
2
0 0 1 0
2 0 -1 0
2
0 0 1 0
2 1 -1 0
 
Sample Output
Case #1: 1.00 0.00
Case #2: 1.00 1.00
 
 给出几个点的坐标和xy方向上的坐标分速度,问什么时候两点之间距离最大值最小,可以想到两点之间距离要么一直增大,要么先减小后增大,三分就可以啦
#pragma GCC diagnostic error "-std=c++11"
//#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

const double eps = 1e-8;
const int N= 300 + 5;
struct point{
    double x, y, vx, vy;
    void read(){ cin >> x >> y >> vx >> vy; }
}p[N];
int n;

double dist_points(point p1, point p2, double t){
    double x = (p1.x + t * p1.vx) - (p2.x + t * p2.vx);
    double y = (p1.y + t * p1.vy) - (p2.y + t * p2.vy);
    return sqrt(x * x + y * y);
}

double cal(double x){
    double Max = 0;
    for(int i = 0; i < n; i++)
        for(int j = i + 1; j < n; j++)
            Max = max(Max, dist_points(p[i], p[j], x));
    return Max;
}

double ternary_search(double L, double R){
    if(L > R) swap(L, R);
    while(R - L > eps){
        double mid1, mid2;
        mid1 = (L + R) / 2;
        mid2 = (mid1 + R) / 2;
        if(cal(mid1) <= cal(mid2)) R = mid2;
        else L = mid1;
    }
    return (L + R) / 2;
}
int main(){ _
    int T, Cas = 0;
    cin >> T;
    while(T --){
        cin >> n;
        for(int i = 0; i < n; i++) p[i].read();
        double x = ternary_search(0, 1e8);
        printf("Case #%d: %.2f %.2f\n", ++Cas, x, cal(x));
    }
}

 

转载于:https://www.cnblogs.com/Pretty9/p/7428188.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值