1. 程式人生 > >2017 CCPC 哈爾濱站 HDU 6242

2017 CCPC 哈爾濱站 HDU 6242

ins sts pri numbers case 是否 frame 隨機 script

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


Problem Description 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.

Input The first line is the number of test cases.

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

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.

Output 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|,R109.

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.

Sample Input 1 7 1 1 1 0 1 -1 0 1 -1 1 0 -1 -1 0 Sample Output 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 }

2017 CCPC 哈爾濱站 HDU 6242