joj 2433: Circle Railway

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K454131Standard

There are some villages in the ACM Mainland. The king want to build a circle railway to make the traffic more easy. What is the best radius if the center of the circle railway is fixed? In the ACM Mainland, all the residenter can fly into the train without station. So you should calculate the summary from the villages directed to the circle railway.

Input

The first line of each case is the number of villages n (n <=100) , n is zero means the end of input. The next n lines includes two double coordinates. The coordinates of the center is shown after villages data.

Output

Output the best summary in one line. The result is displayed to a precision of 3 digit after the decimal point.

Sample Input

2
1.0 1.0
2.0 2.0
0.0 0.0
0

Sample Output

1.414

/*

最简单的暴力枚举所有点,将所有点到圆心的距离求出,将其放到同一直线上讨论,不难证出所有best summary中一定有一种情况在其中某点上,否则一定存在更小的best summary,所以枚举所有点即可,找出最小的距离,这里有个小技巧,就是总距离只有一个单调减区间和一个增区间,也就是说发现所记录的answer比tmp小的时候就可以停止搜索,公式tmp=tmp+(2*i-n)*(d[i]-d[i-1]);
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define sqr(a) ((a)*(a))
#define dis2(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
using namespace std;
const int pi=acos(-1.0);
struct point
{
    double x,y;
}p[105],cir;
double r;

int main ()
{
    int n;
    while (scanf("%d",&n) && n)
    {
        double d[105],sum=0.0,ans=0.0,tmp=0.0;
        for (int i=0 ; i<n ; i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        scanf("%lf%lf",&cir.x,&cir.y);
        for (int i=0 ; i<n ; i++)
        {
           d[i]=dis2(p[i],cir);
           sum+=d[i];
        }
        sort(d,d+n);//将距离从小到大排序
        ans=sum-n*d[0];
        tmp=ans;
        for (int i=1 ; i<n ; i++)
        {
            //printf("%lf ",tmp);
            tmp=tmp+(2*i-n)*(d[i]-d[i-1]);
            if (tmp<ans)ans=tmp;
        }
        printf("%.3lf/n",ans);
   }
    return 0;
}
/*
3
1.0 1.0
2.0 2.0
4.0 4.0
0.0 0.0
2
1.0 1.0
2.0 2.0
0.0 0.0

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值