您的位置:首页 > 其它

2017 CCPC 哈尔滨站 HDU 6242

2017-11-17 18:43 369 查看

Geometry Problem

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1091 Accepted Submission(s): 208
Special Judge


[align=left]Problem Description[/align]
Alice is interesting in computation geometry problem recently. She found a interesting problem and solved it easily. Now she will give this problem to you :

You are given N distinct points (Xi,Yi) on the two-dimensional plane. Your task is to find a point P and a real number R, such that for at least ⌈N2⌉ given points, their distance to point P is equal to R.

[align=left]Input[/align]
The first line is the number of test cases.

For each test case, the first line contains one positive number N(1≤N≤105).

The following N lines describe the points. Each line contains two real numbers Xi and Yi (0≤|Xi|,|Yi|≤103) indicating one give point. It's guaranteed that Npoints are distinct.

[align=left]Output[/align]
For each test case, output a single line with three real numbers XP,YP,R, where (XP,YP) is the coordinate of required point P. Three real numbers you output should satisfy 0≤|XP|,|YP|,R≤109.

It is guaranteed that there exists at least one solution satisfying all conditions. And if there are different solutions, print any one of them. The judge will regard two point's distance as R if it is within an absolute error of 10−3 of R.

[align=left]Sample Input[/align]

1

7
1 1

1 0
1 -1
0 1

-1 1

0 -1
-1 0

[align=left]Sample Output[/align]

0 0 1
题意 给出n个点 确定一个圆的圆心和半径 使得至少n/2个点(向上取整)在该圆上 对于每组样例至少有一个解

解析 我们知道 在n个点中每个点在圆上的概率都为0.5 三个不共线的点确定一个外接圆 我们随机取三个点 这三个点的外接圆满足条件的概率为0.5*0.5*0.5=0.125

每次随机消耗的时间复杂度为1e5 枚举1秒内可以100 次 基本可以得到答案

AC代码

1 #include <cstdio>
2 #include <cmath>
3 #include <cstring>
4 #include <cstdlib>
5 #include <iostream>
6 #include <sstream>
7 #include <algorithm>
8 #include <string>
9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn= 1e5+10;
13 const double eps= 1e-6;
14 const int inf = 0x3f3f3f3f;
15 typedef long long ll;
16 struct point
17 {
18     double x,y;
19 }a[maxn];
20 int n;
21 double dis(point a,point b)   //两点间距离
22 {
23     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
24 }
25 bool waijie(point p1,point p2,point p3,point &ans)   //引用修改圆心的值
26 {
27     if(fabs((p3.y-p2.y)*(p2.x-p1.x)-(p2.y-p1.y)*(p3.x-p2.x))<=eps)return false;  //三点共线 没有外接圆
28     double Bx = p2.x - p1.x, By = p2.y - p1.y;            //外接圆板子
29     double Cx = p3.x - p1.x, Cy = p3.y - p1.y;
30     double D = 2 * (Bx * Cy - By * Cx);
31     double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x;
32     double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y;
33     ans.x=cx,ans.y=cy;
34     return true;
35 }
36 bool check(point mid,double d)     //检查是否有n/2个点在外接圆上
37 {
38     int ans=0;
39     for(int i=1;i<=n;i++)
40     {
41         if(fabs(dis(a[i],mid)-d)<=eps)
42             ans++;
43         if((ans+(n-i))*2<n)       //简单优化一下 如果还未判断的点的数量加上已经满足条件的点的数量小于n/2 false
44             return false;
45     }
46     if(ans*2>=n)
47         return true;
48     return false;
49 }
50 int main()
51 {
52     int t;
53     scanf("%d",&t);
54     while(t--)
55     {
56         scanf("%d",&n);
57         for(int i=1;i<=n;i++)
58             scanf("%lf%lf",&a[i].x,&a[i].y);
59         if(n<=2)                                   //n小于等于4特判
60         {
61             printf("%lf %lf %lf\n",a[1].x,a[1].y,0.0);
62             continue;
63         }
64         else if(n<=4)
65         {
66             printf("%lf %lf %lf\n",(a[1].x+a[2].x)/2,(a[1].y+a[2].y)/2,dis(a[1],a[2])/2);
67             continue;
68         }
69         while(true)
70         {
71             point aa=a[rand()%n+1],bb=a[rand()%n+1],cc=a[rand()%n+1];   //随机产生3个点
72             point xin;
73             if(!waijie(aa,bb,cc,xin))
74                 continue;
75             double r=dis(aa,xin);
76             if(check(xin,r))
77             {
78 //                printf("%lf %lf\n",aa.x,aa.y);
79 //                printf("%lf %lf\n",bb.x,bb.y);
80 //                printf("%lf %lf\n",cc.x,cc.y);
81                 printf("%lf %lf %lf\n",xin.x,xin.y,r);
82                 break;
83             }
84         }
85     }
86     return 0;
87 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: