题目链接:
http://poj.org/problem?id=3873
题解:
给你n点的坐标,让你求出这些点到x轴上一点的最大值的最小值。让你输出这个点在x轴上的x的坐标,以及离这个点最远的点的距离。
本来以为是模拟退火,结果有人说是用三分做出来的。(今天好好学了一波三分)
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5*1e5+10;
#define inf 1e18
#define met(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
struct node
{
double x,y;
}num[maxn];
int n;
double cal_sum(double x,double y)
{
double ans=0;
for(int i=0;i<n;i++)
ans=max(sqrt((num[i].x-x)*(num[i].x-x)+(num[i].y-y)*(num[i].y-y)),ans);
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=0)
{
double MAX=-inf,MIN=inf;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&num[i].x,&num[i].y);
MAX=max(MAX,num[i].x);
MIN=min(MIN,num[i].x);
}
double left=MIN,right=MAX,mid1=0,mid2=0;
while(left+eps<right)
{
mid1=(left*2+right)/3;
mid2=(right*2+left)/3;
if(cal_sum(mid1,0)<cal_sum(mid2,0))
right=mid2;
else
left=mid1;
}
//printf("%f\n",right);
printf("%.9f %.9f\n",left,cal_sum(left, 0));
}
}