HDU 6242 Geometry Problem(计算几何 + 随机化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6242
思路:当 n == 1 时 任取一点 p 作为圆心即可。
n >= 2 && n < 5 时 此时有可能出现所有点共线,所以取任意俩点间中点作为圆的圆心。
n >= 5 保证了有解。所以不可能有所有点共线的情况,随机取三个点在正解圆上的概率是 1/8,还是蛮大的...。
外学了下随机算法的写法....时间种子 time(0)要强制转成int,不然会WA,不造为啥....
AC代码:
#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-;
const double INF = 1e18;
const int maxn = 1e5 + ;
int sgn(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x; y = _y;
}
void input()
{
scanf("%lf %lf", &x, &y);
}
bool operator ==(const Point &b) const{
return sgn(x - b.x) == && sgn(y - b.y) == ;
}
bool operator <(const Point &b) const{
return sgn(x - b.x) == ? sgn(y - b.y) < : x < b.x;
}
Point operator -(const Point &b) const{
return Point(x - b.x, y - b.y);
}
Point operator +(const Point &b) const{
return Point(x + b.x, y + b.y);
}
double operator ^(const Point &b) const{
return x*b.y - b.x*y;
}
double operator *(const Point &b) const{
return x*b.x + y*b.y;
}
Point operator*(const double &k)const{
return Point(x*k,y*k);
}
Point operator/(const double &k)const{
return Point(x/k,y/k);
}
Point rotleft(){
return Point(-y, x);
}
double distance(Point p)
{
return hypot(x - p.x,y - p.y);
}
} p[maxn];
struct Line{
Point s, e;
Line(){}
Line(Point _s, Point _e){
s = _s, e = _e;
}
Point crosspoint(Line v){
double a1 = (v.e - v.s) ^ (s - v.s);
double a2 = (v.e - v.s) ^ (e - v.s);
return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
}
};
struct circle{
Point p;
double r;
circle(){}
circle(Point a, Point b, Point c){
Line u = Line( (a + b) / , ( (a + b) / ) + ( (b - a).rotleft() ));
Line v = Line( (b + c) / , ( (b + c) / ) + ( (c - b).rotleft() ));
p = u.crosspoint(v);
r = p.distance(a);
}
int relation(Point b){
double dst = b.distance(p);
if(sgn(dst - r)<) return ;
else if(sgn(dst - r) == ) return ;
return ;
}
};
int main()
{
int t;
scanf("%d",&t);
srand((int)time());
while(t--)
{
int n;
scanf("%d",&n);
for(int i = ;i < n;i++) p[i].input();
if(n == ) printf("0.000 0.000 %.6f\n",p[].distance(Point(,)));
else if(n >= && n < ){
Point v = (p[] + p[])/;
printf("%.6f %.6f %.6f\n", v.x, v.y, 0.5*p[].distance(p[]));
}
else{
while(){
int a = rand() % n, b = a, c = a;
while(b = rand() % n){
if(b != a) break;
}
while(c = rand() % n){
if(c != a && c != b) break;
}
circle C = circle(p[a], p[b], p[c]);
int num = ;
for(int i = ;i < n;i++)
{
if(C.relation(p[i]) == ) num++;
}
if(num >= (n + ) / ){
printf("%.6f %.6f %.6f\n",C.p.x ,C.p.y, C.r);
break;
}
}
}
}
return ;
}
HDU 6242 Geometry Problem(计算几何 + 随机化)的更多相关文章
- HDU - 6242 Geometry Problem (几何,思维,随机)
Geometry Problem HDU - 6242 Alice is interesting in computation geometry problem recently. She found ...
- hdu 6242 Geometry Problem
Geometry Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other ...
- Codeforces Gym 100338B Geometry Problem 计算几何
Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- Hdu-6242 2017CCPC-哈尔滨站 M.Geometry Problem 计算几何 随机
题面 题意:给你n个点,让你找到一个圆,输出圆心,和半径,使得有超过一半的点刚好在圆上.n<=1e5,题目保证了有解 题解:刚开始看着很不可做的样子,但是多想想,三点确定一个圆,三点啊! 现在有 ...
- 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 - 6242:Geometry Problem(随机+几何)
Alice is interesting in computation geometry problem recently. She found a interesting problem and s ...
- HDU 5572 An Easy Physics Problem (计算几何+对称点模板)
HDU 5572 An Easy Physics Problem (计算几何) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5572 Descripti ...
- 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/ ...
随机推荐
- Hibernate 异常org.hibernate.LazyInitializationException: could not ini...
错误页面提示 could not initialize proxy - no Session 控制台 org.hibernate.LazyInitializationException: could ...
- 《JavaScript DOM 编程艺术》学习成果
(在线演示地址)[http://thqy39.github.io/works/03.Js%20DOM%20website/index.html]
- BPT(Business Process Testing)
- Flask-Scrip
介绍及安装 Flask-Script是一个让你的命令行支持自定义命令的工具,它为Flask程序添加一个命令行解释器.可以让我们的程序从命令行直接执行相应的程序. 安装 pip install Flas ...
- 详解 CSS 绝对定位
基本定义和用法 在 CSS 中,position 属性指定一个元素(静态的,相对的,绝对或固定,以及粘性定位)的定位方法的类型. 当设置 position 属性的值为 absolute 时,生成绝对定 ...
- boby标签中相关标签
有关字体相关的标签 h1~h6 字体大小 标题使用<h1>至<h6>标签进行定义,<h1>定义最大的标题,<h6>定义最小的标题.具有 align属 ...
- firmware
路由器固件分析题,首先要安装firmware-mod-kit 安装命令: linux> sudo apt-get install git build-essential zlib1g-dev l ...
- 容斥原理+补集转化+MinMax容斥
容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...
- 【leetcode】937. Reorder Log Files
题目如下: You have an array of logs. Each log is a space delimited string of words. For each log, the f ...
- VIM的一些使用积累
替换: :s/cst/dst/gc 黏贴后格式不对齐: gg=G 全选并黏贴 gg :"+yG