由于牛牛战队经常要外出比赛,因此在全国各地建立了很多训练基地,每一个基地都有一个坐标(x,y)。
这周末,牛牛队又要出去比赛了,各个比赛的赛点都在x轴上。牛牛战队为了方便比赛,想找一个到达训练基地最大距离最小的地方作为比赛地。
分析:
设最大距离为f(x),可以推断f(x)的图像是一个单峰的图像,所以可以三分。
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e5 + 10;
struct node {
int x, y;
}an[maxn];
int n;
double cheak(double x)
{
double ret = 0;
for (int i = 1; i <= n; i++) {
ret = max(ret, sqrt((an[i].x - x) * (an[i].x - x) + an[i].y * an[i].y));
}
return ret;
}
int main(void)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d", &an[i].x, &an[i].y);
double l = -10000, r = 10000;
while (r - l > 1e-11) { //三分
double midl = l + (r - l) / 3;
double midr = r - (r - l) / 3;
if (cheak(midl) > cheak(midr)) l = midl;
else r = midr;
}
printf("%.4f\n", cheak(r));
return 0;
}