码迷,mamicode.com
首页 > 其他好文 > 详细

2017 CCPC 哈尔滨站 HDU 6242

时间:2017-11-17 19:58:29      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:others   mat   other   string   条件   简单   --   reg   contain   

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 PThree 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

标签:others   mat   other   string   条件   简单   --   reg   contain   

原文地址:http://www.cnblogs.com/stranger-/p/7852955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!