HDU 6242 Geometry Problem(2017 CCPC 哈理工)(随机化+计算几何)
来源:互联网 发布:启信宝数据采集 编辑:程序博客网 时间:2024/05/22 15:45
题目
题意:
在一个二维平面上,给出至多1e5的点,让你构造出一个点和一个距离,使得这些点中有一半(向上取整)以上的点到你构造的点的距离等于你给出的距离。
思路:
网上看到有人用随机化算法过,想了想,挺有道理。
构成一个圆只需要三个不共线的点就可以了,题目中保证了存在这样一个圆,而且有一半以上的点在这个圆上,假设现在有10000个点,有5000个点在一个圆上,那么一次取出三个点在圆上的概率为:C(5000,3)/C(10000,3),大致是1/8,那么一次取出不符合题意的概率是7/8,循环跑上100次的时候脸黑的概率已经降低到1e-6了,肯定能找到。
(虽说随机数并不随机,但在1e5以内还是可以保证随机性的)
那么做法就是任取三点找所构成的圆的圆心,确定半径,遍历点。
PS:求出圆心后一定要判断大小,大于1e9重新随机。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N=1e5+10;const double eps=1e-6;struct Point{ double x,y; Point(double _x,double _y) { x=_x;y=_y; } Point(){}}a[N];Point waixin(Point a,Point b,Point c){ double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2; double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2; double d=a1*b2-a2*b1; return Point(a.x+(c1*b2-c2*b1)/d,a.y+(a1*c2-a2*c1)/d);}double dis(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int n;void solve(){ int fin=(n+1)/2; while(1) { int b=rand()%n; int c=rand()%n; int d=rand()%n; if(c==b&&d==c)continue; Point xin=waixin(a[b],a[c],a[d]); double distance=dis(xin,a[b]); if(fabs(xin.x)>1e9||fabs(xin.y)>1e9||distance>1e9)continue; int k=0; for(int i=0;i<n;i++) { if(fabs(dis(a[i],xin)-distance)<eps) k++; if(k==fin) { printf("%.10lf %.10lf %.10lf\n",xin.x,xin.y,distance); return; } } }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); if(n>=5) solve(); else { if(n==1) printf("%.10lf %.10lf %.10lf\n",a[0].x+1,a[0].y,1.0); else printf("%.10lf %.10lf %.10lf\n",(a[0].x+a[1].x)/2,(a[0].y+a[1].y)/2,dis(a[0],a[1])/2); } } return 0;}
阅读全文
0 0
- HDU 6242 Geometry Problem(2017 CCPC 哈理工)(随机化+计算几何)
- HDU 6242/2017CCPC哈尔滨 Geometry Problem 【随机化+计算几何】
- HDU-6242:Geometry Problem(随机化+几何)
- 2017 CCPC 哈尔滨 M ( hdu 6242) Geometry Problem (随机化 + 三角形外心)
- hdu 6242 Geometry Problem(随机化+三点求外接圆)
- hdu 1086 You can Solve a Geometry Problem too(计算几何水题)
- HDU 1086 You can Solve a Geometry Problem too(计算几何)
- HDU 1086 You can Solve a Geometry Problem too (计算几何 判断线段相交)
- 2017CCPC哈尔滨 M:Geometry Problem(随机)
- HDU 6242 Geometry Problem (随机数,几何)
- 【2016-CCPC-H】计算几何(Special Tetrahedron,hdu 5839)
- HDU 5858 Hard problem(计算几何)
- HDU 5858 Hard problem(计算几何)
- HDU-5858 Hard problem(计算几何)
- 2017 CCPC 哈尔滨站 --M --- Geometry Problem
- 计算几何(一道题) Geometry
- You can Solve a Geometry Problem too(计算几何_求线段相交)
- hdu 1086 You can Solve a Geometry Problem too(几何(线段相交))
- linux下的文件目录和存储结构总结
- Redis持久化(RDB+AOF)与容灾备份
- 使用js控制banner宽高
- vim-ide 脚本实现“一键安装”
- 十月十五日课堂笔记
- HDU 6242 Geometry Problem(2017 CCPC 哈理工)(随机化+计算几何)
- iOS KVC 和 KVO 原理实现
- Java程序通过JDBC连接远程的数据库SQL Server
- var、let、const 区别?
- tensorflow google实战 学习笔记——TensorFlow入门(2)
- TensorFlow在MNIST中的应用 识别手写数字(OpenCV+TensorFlow+CNN)
- 树莓派-概述和使用-下载和安装
- Qt中“调试器未设置”的解决方法
- 刷题中java的简单应用