码迷,mamicode.com
首页 > 其他好文 > 详细

BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】

时间:2015-04-28 22:38:57      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:

ial Judge
Type: 
None
 
 

Johnny and his friends have decided to spend Halloween night doing the usual candy collection from the households of their village. As the village is too big for a single group to collect the candy from all houses sequentially, Johnny and his friends have decided to split up so that each of them goes to a different house, collects the candy (or wreaks havoc if the residents don‘t give out candy), and returns to a meeting point arranged in advance.

There are n houses in the village, the positions of which can be identified with their Cartesian coordinates on the Euclidean plane. Johnny‘s gang is also made up of n people (including Johnny himself). They have decided to distribute the candy after everybody comes back with their booty. The houses might be far away, but Johnny‘s interest is in eating the candy as soon as possible.

Keeping in mind that, because of their response to the hospitality of some villagers, some children might be wanted by the local authorities, they have agreed to fix the meeting point by the river running through the village, which is the line y = 0. Note that there may be houses on both sides of the river, and some of the houses may be houseboats (y = 0). The walking speed of every child is 1 meter per second, and they can move along any direction on the plane.

At exactly midnight, each child will knock on the door of the house he has chosen, collect the candy instantaneously, and walk back along the shortest route to the meeting point. Tell Johnny at what time he will be able to start eating the candy.

 

Input

 Each test case starts with a line indicating the number n of houses ( 1<=n<=50 000). The next n lines describe the positions of the houses; each of these lines contains two floating point numbers x and y ( -200 000 <= xy <= 200 000), the coordinates of a house in meters. All houses are at different positions.

A blank line follows each case. A line with n = 0 indicates the end of the input; do not write any output for this case.

 

Output

For each test case, print two numbers in a line separated by a space: the coordinate x of the meeting point on the line y = 0 that minimizes the time the last child arrives, and this time itself (measured in seconds after midnight). Your answer should be accurate to within an absolute or relative error of 10-5.

 

Sample Input

2
1.5 1.5
3 0

1
0 0

4
1 4
4 4
-3 3
2 4

5
4 7
-4 0
7 -6
-2 4
8 -5

0

Sample Output

1.500000000 1.500000000
0.000000000 0.000000000
1.000000000 5.000000000
3.136363636 7.136363636

题目大意:有n个人要回到x上的某个聚集点,问所有人都回到该点的最短时间。
解题思路:利用三分,求出x点坐标,最后求出最远的点到该点的距离。


#include<bits/stdc++.h>
using namespace std;
struct Cor{
    double x,y;
}cor[55000];
#define mid (L+R)/2.0
#define mid_L (mid+L)/2.0
const double eps=1e-10;
const double INF=1e9;
int n;
double dis(Cor a,Cor b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double calcu(double tx){
    double ret=-INF;
    Cor tmp_;
    tmp_.x=tx,tmp_.y=0;
    for(int i=0;i<n;i++){
        if(ret<dis(cor[i],tmp_)){
            ret=dis(cor[i],tmp_);
        }
    }
    return sqrt(ret);
}
double three_div(double L,double R){
    while(R-L>eps){
        if(calcu(mid)>calcu(mid_L)){
            R=mid;
        }else{
            L=mid_L;
        }
    }
    return mid;
}
int main(){
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&cor[i].x,&cor[i].y);
        }
        double ans_x,ans_d;
        ans_x= three_div(-200000.0,200000.0);
        ans_d=calcu(ans_x);
        printf("%.9lf %.9lf\n",ans_x,ans_d);
    }
    return 0;
}

  

BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】

标签:

原文地址:http://www.cnblogs.com/chengsheng/p/4464038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!