1336: [Balkan2002]Alien最小圆覆盖

1337: 最小圆覆盖

Time Limit: 1 Sec Memory Limit: 162 MBSec Special Judge

Description

给出N个点,让你画一个最小的包含所有点的圆。

Input

先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0)

Output

输出圆的半径,及圆心的坐标

Sample Input

6

8.0 9.0

4.0 7.5

1.0 2.0

5.1 8.7

9.0 2.0

4.5 1.0

Sample Output

5.00

5.00 5.00

这两道题是最小圆覆盖的裸题,这里主要讲讲求最小圆覆盖的思路。

最小圆覆盖问题就是让你求一个最小的圆使得它能够刚好覆盖掉给出的点。本蒟蒻学习的是一种期望效率O(n)" role="presentation" style="position: relative;">O(n)O(n)的算法。

那么我们怎么做呢?

首先,我们假设前i−1" role="presentation" style="position: relative;">i−1i−1个点已经被现在的圆给覆盖掉了,现在正在处理第i" role="presentation" style="position: relative;">ii个点,那么我们check" role="presentation" style="position: relative;">checkcheck一波,如果当前点在圆外,我们就重构覆盖前i" role="presentation" style="position: relative;">ii个点的最小覆盖圆,具体操作就是先以i" role="presentation" style="position: relative;">ii为圆心作圆,然后从前i−1" role="presentation" style="position: relative;">i−1i−1个点中找一个点j" role="presentation" style="position: relative;">jj使得j" role="presentation" style="position: relative;">jj在圆外,此时最小覆盖圆应该为i,j" role="presentation" style="position: relative;">i,ji,j中点,然后我们再从前j−1" role="presentation" style="position: relative;">j−1j−1个点中找一个点k" role="presentation" style="position: relative;">kk使得k" role="presentation" style="position: relative;">kk在圆外,这时最小覆盖圆自然应该是三角形ijk" role="presentation" style="position: relative;">ijkijk的外接圆。我们一直这样操作到前i" role="presentation" style="position: relative;">ii个点都能够check" role="presentation" style="position: relative;">checkcheck成功为止。

但这样的话最坏情况不应该是O(n3)" role="presentation" style="position: relative;">O(n3)O(n3)吗?是的,因此我们要在最开始的时候将点随机打乱,这样的话期望的效率就应该是O(n)" role="presentation" style="position: relative;">O(n)O(n)的了。还有就是注意T1336" role="presentation" style="position: relative;">T1336T1336应该保留10" role="presentation" style="position: relative;">1010位小数而不是题目上说的2" role="presentation" style="position: relative;">22位。

T1336" role="presentation" style="position: relative;">T1336T1336代码:

#include<bits/stdc++.h>
#define N 100005
using namespace std;
struct point{double x,y;}p[N],O;
struct line{double a,b,c;};
double r;
inline double mul(double x){return x*x;}
inline double dis(point x,point y){return sqrt(mul(x.x-y.x)+mul(x.y-y.y));}
inline bool in_check(point x){return dis(O,x)<=r;}
inline point calc(line a,line b){return point{(b.c*a.b-a.c*b.b)/(a.a*b.b-a.b*b.a),(b.c*a.a-a.c*b.a)/(a.b*b.a-b.b*a.a)};}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
    random_shuffle(p+1,p+n+1);
    r=0;
    for(int i=1;i<=n;++i){
        if(in_check(p[i]))continue;
        O.x=p[i].x,O.y=p[i].y,r=0;
        for(int j=1;j<i;++j){
            if(in_check(p[j]))continue;
            O.x=(p[i].x+p[j].x)/2,O.y=(p[i].y+p[j].y)/2;
            r=dis(O,p[i]);
            for(int k=1;k<j;++k){
                if(in_check(p[k]))continue;
                O=calc(line{2*(p[i].x-p[k].x),2*(p[i].y-p[k].y),mul(p[k].x)+mul(p[k].y)-mul(p[i].x)-mul(p[i].y)},line{2*(p[i].x-p[j].x),2*(p[i].y-p[j].y),mul(p[j].x)+mul(p[j].y)-mul(p[i].x)-mul(p[i].y)});
                r=dis(p[i],O);
            }
        }
    }
    printf("%.10lf\n%.10lf %.10lf",r,O.x,O.y);
    return 0;
}

T1337" role="presentation" style="position: relative;">T1337T1337代码:

#include<bits/stdc++.h>
#define N 100005
using namespace std;
struct point{double x,y;}p[N],O;
struct line{double a,b,c;};
double r;
inline double mul(double x){return x*x;}
inline double dis(point x,point y){return sqrt(mul(x.x-y.x)+mul(x.y-y.y));}
inline bool in_check(point x){return dis(O,x)<=r;}
inline point calc(line a,line b){return point{(b.c*a.b-a.c*b.b)/(a.a*b.b-a.b*b.a),(b.c*a.a-a.c*b.a)/(a.b*b.a-b.b*a.a)};}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
    random_shuffle(p+1,p+n+1);
    r=0;
    for(int i=1;i<=n;++i){
        if(in_check(p[i]))continue;
        O.x=p[i].x,O.y=p[i].y,r=0;
        for(int j=1;j<i;++j){
            if(in_check(p[j]))continue;
            O.x=(p[i].x+p[j].x)/2,O.y=(p[i].y+p[j].y)/2;
            r=dis(O,p[i]);
            for(int k=1;k<j;++k){
                if(in_check(p[k]))continue;
                O=calc(line{2*(p[i].x-p[k].x),2*(p[i].y-p[k].y),mul(p[k].x)+mul(p[k].y)-mul(p[i].x)-mul(p[i].y)},line{2*(p[i].x-p[j].x),2*(p[i].y-p[j].y),mul(p[j].x)+mul(p[j].y)-mul(p[i].x)-mul(p[i].y)});
                r=dis(p[i],O);
            }
        }
    }
    printf("%.3lf",r);
    return 0;
}

2018.07.04 BZOJ1336&&1337: Balkan2002Alien最小圆覆盖的更多相关文章

  1. BZOJ1336 Balkan2002 Alien最小圆覆盖 【随机增量法】*

    BZOJ1336 Balkan2002 Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000, ...

  2. 2018.07.04 BZOJ 2823: AHOI2012信号塔(最小圆覆盖)

    2823: [AHOI2012]信号塔 Time Limit: 10 Sec Memory Limit: 128 MB Description 在野外训练中,为了确保每位参加集训的成员安全,实时的掌握 ...

  3. Bzoj 1336&1337 Alien最小圆覆盖

    1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1473  ...

  4. bzoj1336: [Balkan2002]Alien最小圆覆盖

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1336 1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 ...

  5. 2018.07.04 POJ 1265 Area(计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description Being well known for its highly innovative ...

  6. 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)

    Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...

  7. 2018.07.04 POJ 1113 Wall(凸包)

    Wall Time Limit: 1000MS Memory Limit: 10000K Description Once upon a time there was a greedy King wh ...

  8. 2018.07.04 POJ 1654 Area(简单计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description You are going to compute the area of a spec ...

  9. 2018.07.04 POJ 3304 Segments(简单计算几何)

    Segments Time Limit: 1000MS Memory Limit: 65536K Description Given n segments in the two dimensional ...

随机推荐

  1. leetcode961

    class Solution: def repeatedNTimes(self, A): doubleN = len(A) N = doubleN / 2 dic = {} for a in A: i ...

  2. J2SE 8的注解

    1. 注解概念 (1) 注解格式 modifiers @interface AnnotationName { type elementName(); type elementName() defaul ...

  3. Spring MVC 确定目标方法POJO 类型参数

    1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...

  4. 16 python xml模块

    1.基本概念 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单. 不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀. 至今很多传统公司如金 ...

  5. 扩展C#与元编程(一)

    众所周知,Roslyn project已经开源一年多了.简单的说,Roslyn是:1)用C#/VB写的C#/VB的编译器,以及与IDE集成:2)编译器的功能以API的方式暴露出来(即一组DLL). R ...

  6. django中怎么使用mysql数据库的事务

    Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起失败. Django中对于数据库的事务,默认每执行 ...

  7. HttpClientUtil 工具类

    /* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...

  8. Mysql 中json 相关函数的使用

    1.JSON_LENGTH: select content from test1 ["1","2","3","4",&q ...

  9. 第七章 二叉搜索树 (d3)AVL树:删除

  10. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...