Geometry Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1722    Accepted Submission(s): 304
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(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 N points 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|,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.
 
Sample Input
1
7
1 1
1 0
1 -1
0 1
-1 1
0 -1
-1 0
 
Sample Output
0 0 1
 
这一题让我学会了随机数法。
 
题意:找出一个圆使至少n/2个点在圆上(数据保证有解)
 
解题思路:用普通的for循环肯定会超,因为数据保证有解,我们随机三个点,这三个点都在圆上的概率是0.5*0.5*0.5=0.125,三个点不都在圆上的概率是7/8,那么随机100次,概率就是(7/8)^100,约为1e-6,基本接近于0了,也就是说随机一百遍基本就能找到三个点都在圆上的情况,也就能找到那个圆的圆心和半径了。
 
注意坑点:1.n<5的情况要另考虑  2.三点共线  3.判断找到圆的条件(我一开始是cnt<=n/2,这样n如果从0开始遍历答案就不对了
 
附ac代码:
 1 #include <iostream>
2 #include <string.h>
3 #include <algorithm>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cmath>
7 using namespace std;
8 typedef long long ll;
9 const int maxn = 1e5+10;
10 struct nod
11 {
12 double x;
13 double y;
14 }nu[maxn];
15 int vis[maxn];
16 double xx1,yy1,xx2,yy2,xx3,yy3;
17 void getr(double &x,double &y,double &r)
18 {
19 // printf("%lf %lf\n",xx2,xx1);
20 double a=2*(xx2-xx1);
21 double b=2*(yy2-yy1);
22 double c=xx2*xx2-xx1*xx1+yy2*yy2-yy1*yy1;
23 double d=2*(xx3-xx2);
24 double e=2*(yy3-yy2);
25 double f=xx3*xx3-xx2*xx2+yy3*yy3-yy2*yy2;
26 x=(b*f-e*c)/(b*d-e*a);
27 y=(a*f-d*c)/(a*e-b*d);
28 r=sqrt((x-xx1)*(x-xx1)+(y-yy1)*(y-yy1));
29 // printf("%lf %lf %lf\n",x,y,r);
30 }
31 int main()
32 {
33 int t;
34 int n;
35 scanf("%d",&t);
36 while(t--)
37 {
38
39 scanf("%d",&n);
40 for(int i=0;i<n;++i)
41 scanf("%lf%lf",&nu[i].x,&nu[i].y);
42 if(n<=2)
43 {
44 printf("%lf %lf %lf\n",nu[0].x,nu[0].y,0.0);
45 }
46 else if(n<=4)
47 {
48 double x,y,r;
49 x=(nu[0].x+nu[1].x)/2;
50 y=(nu[0].y+nu[1].y)/2;
51 r=sqrt((x-nu[0].x)*(x-nu[0].x)+(y-nu[0].y)*(y-nu[0].y));
52 printf("%lf %lf %lf\n",x,y,r);
53 }
54 else
55 {
56 while (true)
57 {
58 int coo1=rand()%n;
59 int coo2=rand()%n;
60 int coo3=rand()%n;
61 if(coo1==coo2 || coo1==coo3 || coo2==coo3) continue;
62 xx1=nu[coo1].x; yy1=nu[coo1].y;
63 xx2=nu[coo2].x; yy2=nu[coo2].y;
64 xx3=nu[coo3].x; yy3=nu[coo3].y;
65 if(fabs((yy3-yy2)*(xx2-xx1)-(xx3-xx2)*(yy2-yy1))<=1e-6)
66 continue;
67 double x=0,y=0,r=0;
68 getr(x,y,r);
69 int cnt=0;
70 for(int i=0;i<n;++i)
71 {
72 if(fabs(r*r- ((nu[i].x-x)*(nu[i].x-x)+(nu[i].y-y)*(nu[i].y-y)) )<=1e-6)
73 ++cnt;
74 }
75 if(cnt*2>=n)
76 {
77 printf("%lf %lf %lf\n",x,y,r);
78 break;
79 }
80 }
81 }
82 }
83 return 0;
84 }

hdu 6242 Geometry Problem的更多相关文章

  1. HDU - 6242 Geometry Problem (几何,思维,随机)

    Geometry Problem HDU - 6242 Alice is interesting in computation geometry problem recently. She found ...

  2. HDU 6242 Geometry Problem(计算几何 + 随机化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6242 思路:当 n == 1 时 任取一点 p 作为圆心即可. n >= 2 && ...

  3. HDU - 6242:Geometry Problem(随机+几何)

    Alice is interesting in computation geometry problem recently. She found a interesting problem and s ...

  4. hdu 1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  5. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

  6. HDU 1086:You can Solve a Geometry Problem too

    pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  7. hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. hdu 1086 You can Solve a Geometry Problem too (几何)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  9. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

随机推荐

  1. Python3.9的http.client.py下的HTTPMessage类中的方法getallmatchingheaders的bug修复建议

    在官方网站已经提交相关issue,不过目前看好像还没有修复.具体的bug位置为: http文件夹下的client.py文件,代码位置为:类HTTPMessage下的方法getallmatchinghe ...

  2. mybatis源码分析之走进缓存

    之前写了一篇关于mybatis缓存的读后感,想了想还是把缓存模块简单分析一下,附赠下载地址:https://github.com/MyBatis/MyBatis-3,github直接搜排名很靠前的. ...

  3. mybatis源码解析之架构理解

    mybatis是一个非常优秀的开源orm框架,在大型的互联网公司,基本上都会用到,而像程序员的圣地-阿里虽然用的是自己开发的一套框架,但其核心思想也无外乎这些,因此,去一些大型互联网公司面试的时候,总 ...

  4. autocommit 隔离级别 next lock gap lock 事务隔离级别和锁

    autocommit 隔离级别 https://www.ibm.com/developerworks/cn/opensource/os-mysql-transaction-isolation-leve ...

  5. https://www.cnblogs.com/wclwcw/p/7515515.html

    https://www.cnblogs.com/wclwcw/p/7515515.html

  6. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError

  7. Springboot中mybatis控制台打印sql语句

    Springboot中mybatis控制台打印sql语句 https://www.jianshu.com/p/3cfe5f6e9174 https://www.jianshu.com/go-wild? ...

  8. HDFS HBase Solr Which one? 从访问模式角度决策

    HDFS 压缩性能最优.扫描速度最快:不支持随机访问,仅支持昂贵.复杂的文件查询 HBase适合随机访问 Solr 适合检索需求 HBase访问单个记录的时间为毫秒级别,而HDFS不支持随机访问. H ...

  9. CF42A

    题意 给定两个序列 a 和 b. 序列 a 中的各个数之间的比例可以得出一个 x . 当 b 中比例满足 a 中比例,即 \(b_1\):\(b_2\):\(b_3\)-- \(=\) \(a_1\) ...

  10. 最简单直接地理解Java软件设计原则之接口隔离原则

    理论性知识 定义 接口隔离原则, Interface Segregation Principle,(ISP). 一个类对应一个类的依赖应该建立在最小的接口上: 建立单一接口,不要建立庞大臃肿的接口: ...