joj 2433: Circle Railway

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 454 131 Standard

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

*/

你可能感兴趣的:(struct,input,Build,each,output)