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 Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).

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));
    }
}
时间: 2024-10-23 15:11:40

HDU-4717 The Moving Points(凸函数求极值)的相关文章

hdu 4717 The Moving Points(三分)

题目链接:hdu 4717 The Moving Points 题意: 在二维平面上有n个点,每个点给出移动的方向和速度. 问在某个时刻,这些点中最大距离最小是多少,输出时刻和距离. 题解: 我们可以知道,每个点对的距离要么是单调递增,要么是有一个峰的函数. 举例画一下可知道合成的这个函数最多只有一个峰,所以可以用三分求解. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespa

HDU 4717 The Moving Points (三分法)

题意:给n个点的坐标的移动方向及速度,问在之后的时间的所有点的最大距离的最小值是多少. 思路:三分.两点距离是下凹函数,它们的max也是下凹函数.可以三分. #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<cstdlib> #include<string> #include<cmath> #include&

hdu 4717 The Moving Points(三分)

http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每个点的坐标以及每个点移动的速度和方向.问在那一时刻点集中最远的距离在所有时刻的最远距离中最小. 比赛时一直以为是计算几何,和线段相交什么的有关.赛后队友说这是道三分,仔细想了想确实是三分,试着画画图发现它是一个凸性函数,存在一个最短距离.然后三分时间就可以了. #include <stdio.h> #include <iostream> #include <map&g

HDU-3714 Error Curves(凸函数求极值)

Error Curves Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 6241    Accepted Submission(s): 2341 Problem Description Josephina is a clever girl and addicted to Machine Learning recently. Shepay

三分法求凸函数的极值

作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4397990.html 在机器学习中,求凸函数的极值是一个常见的问题,常见的方法如梯度下降法,牛顿法等,今天我们介绍一种三分法来求一个凸函数的极值问题. 对于如下图的一个凸函数$f(x),x\in [left,right]$,其中lm和rm分别为区间[left,right]的三等分点,我们发现如果f(lm)<f(rm),那么函数值最小的点的横坐标x一定在[left,rm]之间.如果x在[rm

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

hihoCoder #1142 : 三分求极值

#1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 提示:三分法 输入 第1行:5个整数a,b,c,x,y.前三个数构成抛物线的参数,后两个数x,y表示P点坐标.-200≤a,b,c,x,y≤200 输出 第1行:1个实数d,保留3位小数(四舍五入) 样例输入 2 8 2 -2 6 样例输出 2.437

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan