传送门:Gym - 102470A
题目描述
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.
输入描述
Each test case starts with a line indicating the number $n$ of houses $(1≤n≤50000)$. The next n lines describe the positions of the houses; each of these lines contains two floating point numbers $x$ and $y$ $(−200000≤x,y≤200000)$, 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.
输出描述
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}$.
思路分析
三分求最小值,这题精度卡的紧,算的过程中不要开方,结果再开方
样例输入
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
样例输出
1.500000000 1.500000000
0.000000000 0.000000000
1.000000000 5.000000000
3.136363636 7.136363636
AC代码
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define vi vector<int>
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100010;
const double eps = 1e-7;
double x[N], y[N];
int n;
int main() {
#ifdef xiaofan
freopen("in.txt", "r", stdin);
#endif
while (~scanf("%d", &n) && n) {
for (int i = 0; i < n; i++)
scanf("%lf %lf", &x[i], &y[i]);
fun<double (double) > f = [&] (double nx) {
double ma = 0;
for (int i = 0; i < n; i++) {
double s = (nx - x[i]) * (nx - x[i]) + y[i] * y[i];
ma = max(ma, s);
}
return ma;
};
double l = -2000000.0, r = 2000000.0;
while (r - l > eps) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
if (f(m1) > f(m2)) {
l = m1;
} else {
r = m2;
}
}
printf("%.7lf %.7lf\n", r, sqrt(f(r)));
}
return 0;
}