HDU - 6242 Geometry Problem (几何,思维,随机)
Geometry Problem
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 NN distinct points (Xi,Yi)(Xi,Yi) on the two-dimensional plane. Your task is to find a point PP and a real number RR, such that for at least ⌈N2⌉⌈N2⌉ given points, their distance to point PP is equal to RR.
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)N(1≤N≤105).
The following NN lines describe the points. Each line contains two real numbers XiXiand YiYi (0≤|Xi|,|Yi|≤103)(0≤|Xi|,|Yi|≤103) indicating one give point. It's guaranteed that NN points are distinct.
Output
For each test case, output a single line with three real numbers XP,YP,RXP,YP,R, where (XP,YP)(XP,YP) is the coordinate of required point PP. Three real numbers you output should satisfy 0≤|XP|,|YP|,R≤1090≤|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 RR if it is within an absolute error of 10−310−3 of RR.
Sample Input
1
7
1 1
1 0
1 -1
0 1
-1 1
0 -1
-1 0
Sample Output
0 0 1
题意:
给n个互补相同的二维坐标点,保证可以找到一个点\(p(x,y)\),满足存在\(ceil(n/2)\) 个点和这个点p的距离相同。
思路:
当n=1时,p可以为任意一点
当\(2<=n<=4\) 时,取任意两点的中点即可,
当n>=5 时,
我们随机3个互补相同的点,并找到以这3个点确定的圆的圆心以及半径R,然后计算有多少个点和这个圆心的距离为R,如果个数*2>=n,就说明该圆心就是要找的点,半径就是距离。
为什么这样可以?
因为保证一定存在解,那么一定有至少\(ceil(n/2)\) 个点在同一个圆上,那么找到3个点都在这个圆上的概率大概就是\((1/2)^3\) 那么期望大概就是8次就可以确定出圆心。
ac代码
#include<bits/stdc++.h>
#include<ctime>
using namespace std;
typedef long long ll;
typedef double ld;
const ld eps = 1e-6;
int sgn(ld x)
{
if(fabs(x)<eps)
return 0;
if(x<0)
return -1;
else
{
return 1;
}
}
struct point
{
ld x,y;
point(){}
point(ld _x,ld _y)
{
x=_x;
y=_y;
}
point operator - (const point &b) const
{
return point(x-b.x,y-b.y);
}
ld operator ^ (const point &b) const
{
return x*b.y-y*b.x;
}
ld operator * (const point &b) const
{
return x*b.x+y*b.y;
}
};
point getpoint(point a,point b,point c,point d)
{
point res;
ld a1,b1,c1,a2,b2,c2;
a1=a.y-b.y,b1=b.x-a.x,c1=a.x*b.y-b.x*a.y;
a2=c.y-d.y,b2=d.x-c.x,c2=c.x*d.y-d.x*c.y;
res.x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
res.y=-(a1*c2-a2*c1)/(a1*b2-a2*b1);
return res;
}
struct line
{
point s,e;
line(){}
line(point _s,point _e)
{
s=_s;
e=_e;
}
pair<int,point> operator & (const line &b)const
{
point res=s;
if(sgn((s-e)^(b.s-b.e))==0)
{
if(sgn((s-b.e)^(b.s-b.e))==0)
{
return make_pair(0,res);
}else{
return make_pair(1,res);
}
}
res = getpoint(s,e,b.s,b.e);
return make_pair(2,res);
}
};
int t;
int n;
point a[100010];
ld R;
line l1,l2;
ld base=2e9;
line getline_(point aa,point bb)
{
ld xc=bb.x-aa.x;
ld yc=bb.y-aa.y;
if(sgn(yc)==0)
{
return line(point(bb.x,base),point(bb.x,-base));
}else
{
ld k=-1*xc/yc;
point mid=point((aa.x+bb.x)*0.5,(aa.y+bb.y)*0.5);
return line(point(mid.x+base,mid.y+base*k),point(mid.x-base,mid.y-base*k));
}
}
ld getdis(point aa,point bb)
{
return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
point c;
bool check(point aa,point bb,point cc)
{
l1=getline_(aa,bb);
l2=getline_(bb,cc);
pair<int,point> res=l1&l2;
if(res.first!=2)
{
return 0;
}else if(res.first==2)
{
c=res.second;
R=getdis(c,aa);
int cnt=0;
for(int i=1;i<=n;++i)
{
if(sgn(fabs(getdis(c,a[i]))-R)==0)
{
// cout<<getdis(c,a[i])<<endl;
cnt++;
}
}
// cout<<" cnt "<<" "<<cnt<<endl;
return cnt*2>=n;
}
}
//#define mp make_pair
//
//map<pair<int,pair<int,int> >,bool > vis;
int main()
{
// ios::sync_with_stdio(false);
// cin>>t;
int x,y;
scanf("%d",&t);
while(t--)
{
// vis.clear();
std::mt19937 rnd(time(NULL));
// cin>>n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lf %lf",&a[i].x,&a[i].y);
// cin>>a[i].x>>a[i].y;
}
if(n==1)
{
c.x=0;
c.y=0;
R=getdis(c,a[1]);
}else if(n<=4)
{
c=point((a[1].x+a[2].x)*0.5,(a[1].y+a[2].y)*0.5);
R=getdis(c,a[1]);
}else
{
while(1)
{
int id1,id2,id3;
id1=rnd()%n+1;
do
{
id2=rnd()%n+1;
}while(id2==id1);
do
{
id3=rnd()%n+1;
}while(id3==id1||id3==id2);
// cout<<id1<<" "<<id2<<" "<<id3<<endl;
if(check(a[id1],a[id2],a[id3]))
{
break;
}
}
}
printf("%.5f %.5f %.5f\n",c.x+eps,c.y+eps,R);
// cout<<fixed<<setprecision(5)<<c.x+eps<<" "<<c.y+eps<<" "<<R<<endl;
}
return 0;
}
HDU - 6242 Geometry Problem (几何,思维,随机)的更多相关文章
- hdu 6242 Geometry Problem
Geometry Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other ...
- HDU 6242 Geometry Problem(计算几何 + 随机化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6242 思路:当 n == 1 时 任取一点 p 作为圆心即可. n >= 2 && ...
- hdu 5605 geometry(几何,数学)
Problem Description There is a point P at coordinate (x,y). A line goes through the point, and inter ...
- HDU - 6242:Geometry Problem(随机+几何)
Alice is interesting in computation geometry problem recently. She found a interesting problem and s ...
- 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 ...
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- 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 ...
- (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/ ...
- 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 ...
随机推荐
- 【C# 开发技巧】 C#中WinForm程序退出方法技巧总结
C#中WinForm程序退出方法技巧总结 一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.Ex ...
- Xena L23网络测试仪Valkyrie使用技巧100例:修改设备管理IP,设备关机 (编号01)
Xena Valkyrie产品提供100M~400Gbps全速率接口速率支持 产品链接 https://xenanetworks.com/valkyrie/ 需求# 1.多个用户如何共享使用一个机箱? ...
- 最新 草花互动java校招面经(含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.草花互动等10家互联网公司的校招Offer,因为某些自身原因最终选择了草花互动.6.7月主要是做系统复习.项目复盘.Leet ...
- vue中的$listeners属性作用
一.当组件的根元素不具备一些DOM事件,但是根元素内部元素具备相对应的DOM事件,那么可以使用$listeners获取父组件传递进来的所有事件函数,再通过v-on="xxxx"绑定 ...
- UiPath工具取得网页上面的数据,写入到csv,Outlook邮件发送
问题描述: 想取得网页上面的股票价格,之后写入到csv文本里面之后添加附件发送邮件. 解决方法: 利用UIPath工具来取得数据,之后写入再发送. 具体步骤: 1.打开网页,之后找到所显示的股票行情的 ...
- 使用github经验
使用github经验 良好的使用习惯,就像是每天来看朋友圈一样,不一定每天都有东西要提交,但是一定要一直有一个 repository 在维护,持续的提交代码.同时也要注意自己的 repository的 ...
- (十一)shiro与ssm整合
所有代码在:here pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...
- 利用贝叶斯算法实现手写体识别(Python)
在开始介绍之前,先了解贝叶斯理论知识 https://www.cnblogs.com/zhoulujun/p/8893393.html 简单来说就是:贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯 ...
- 组装技术的新进展 New advances in sequence assembly.
组装技术的新进展 1.测序和组装 很难想象今天距离提出测序和组装已经有40年啦.我们回头来看一下这个问题. “With modern fast sequencing techniques and su ...
- java封装数据类型——Integer 缓存策略验证
上一篇学习 Integer 类型源码,知道了它使用缓存策略,默认对 [-128, 127] 范围的对象进行类加载时自动创建缓存. Integer 源码学习:https://www.cnblogs.co ...