[HDU3007]Buried memory(最小圆覆盖)

题目描述

传送门

题解

三个点能确定一个圆,答案一定为某一个三角形的外接圆
有一种非常神奇的复杂度为 O(n) 的随机增量法解决这个问题
算法流程

  • 先对点集random_shuffle处理一下
  • 假设已经有了前i-1个点的最小圆覆盖 Ci1 ,检验点 pi 是否在 Ci1 内,如果在,则 Ci=Ci1 ,否则, pi 一定在圆 Ci 上,进行下一步
  • 包括点 pi 在内的前j-1个点(j < <script type="math/tex" id="MathJax-Element-141"><</script>i)的最小圆覆盖为 Cj1 ,检验点 pj 是否在 Cj1 内,如果在,则 Cj=Cj1 ,否则, pj 一定在圆 Cj 上,进行下一步
  • 包括点 pi 和点 pj 在内的前k-1个点(k < <script type="math/tex" id="MathJax-Element-150"><</script>j)的最小圆覆盖为 Ck1 ,检验点 pk 是否在 Ck1 内,如果在,则 Ck=Ck1 ,否则, pk 一定在圆 Ck 上。返回

时间复杂度证明
目测复杂度是 O(n3) 的,实际上是 O(n)

  • 首先最后一层循环是 O(j)
  • 考虑倒数第二层循环,这个点有 j3 的概率不在圆内,这个时候要进行一个复杂度为 O(j) 的循环,有 2j3 的概率不进行这个循环,所以期望是 O(1) ,总体复杂度 O(n)
  • 同理可得,第一层循环的复杂度是 O(n)

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 505

const double pi=acos(-1.0);
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double b) {return Vector(a.x*b,a.y*b);}
Vector operator / (Vector a,double b) {return Vector(a.x/b,a.y/b);}

int n;
double r;
Point p[N],c;

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
Vector rotate(Vector a,double rad)
{
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
Point center(Point A,Point B,Point C)
{
    Vector v,w;
    v=B-A,w=C-A;
    v=rotate(v,pi/2),w=rotate(w,pi/2);
    return GLI((A+B)/2,v,(A+C)/2,w);
}
void mcc()
{
    random_shuffle(p+1,p+n+1);
    c=p[1],r=0;
    for (int i=2;i<=n;++i)
        if (dcmp(Len(c-p[i])-r)>0)
        {
            c=p[i],r=0;
            for (int j=1;j<i;++j)
                if (dcmp(Len(c-p[j])-r)>0)
                {
                    c=(p[i]+p[j])/2;
                    r=Len(c-p[i]);
                    for (int k=1;k<j;++k)
                        if (dcmp(Len(c-p[k])-r)>0)
                        {
                            c=center(p[i],p[j],p[k]);
                            r=Len(c-p[i]);
                        }
                }
        }
}
int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        for (int i=1;i<=n;++i)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        mcc();
        printf("%.2lf %.2lf %.2lf\n",c.x,c.y,r);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值