[随机] 最小圆覆盖 BZOJ 2823 1337 1336

期望O(n) 棒极了


BZOJ 2823

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define eps 1e-8
#define X first
#define Y second
using namespace std;
typedef pair<double,double> Point;

int n;
Point P[1000005],C;
double R;

inline double sqr(double x) { return x*x; }

inline int dcmp(double a,double b)
{
	if (fabs(a-b)<eps) return 0;
	if (a<b) return -1; return 1;
}

inline double Dist(Point A,Point B)
{
	return sqrt((A.X-B.X)*(A.X-B.X)+(A.Y-B.Y)*(A.Y-B.Y));
}

inline Point Get(Point a,Point b,Point c)
{
      double a1,a2,b1,b2,c1,c2;
      Point ans;
      a1=2*(b.X-a.X),b1=2*(b.Y-a.Y),c1=sqr(b.X)-sqr(a.X)+sqr(b.Y)-sqr(a.Y);
      a2=2*(c.X-a.X),b2=2*(c.Y-a.Y),c2=sqr(c.X)-sqr(a.X)+sqr(c.Y)-sqr(a.Y);
      if(!dcmp(a1,0)) 
      { 
           ans.Y=c1/b1; 
           ans.X=(c2-ans.Y*b2)/a2; 
      } 
      else if(!dcmp(b1,0)) 
      { 
           ans.X=c1/a1; 
           ans.Y=(c2-ans.X*a2)/b2; 
      } 
      else
      { 
          ans.X=(c2*b1-c1*b2)/(a2*b1-a1*b2); 
          ans.Y=(c2*a1-c1*a2)/(b2*a1-b1*a2); 
      } 
      return ans; 
}

inline Point Get(Point A,Point B)
{
	return Point((A.X+B.X)/2,(A.Y+B.Y)/2);
}

inline void MinCover()
{
	C=P[1]; R=0;
    for(int i=2;i<=n;i++)
    	if (dcmp(Dist(P[i],C),R)>0)
		{
			C=P[i];
			R=0;
        	for (int j=1;j<=i;j++)
				if (dcmp(Dist(P[j],C),R)>0)
				{
					C=Get(P[i],P[j]);
					R=Dist(C,P[i]);
            		for (int k=1;k<=j;k++)
						if (dcmp(Dist(P[k],C),R)>0)
						{
							C=Get(P[i],P[j],P[k]);
							R=Dist(C,P[i]);
						}
				}
		}
}

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%lf%lf",&P[i].X,&P[i].Y);
	MinCover();
	printf("%.2lf %.2lf %.2lf\n",C.X,C.Y,R);
	return 0;
}


BZOJ 1337 1336

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define eps 1e-8
#define X first
#define Y second
using namespace std;
typedef pair<double,double> Point;

int n;
Point P[100005],C;
double R;

inline double sqr(double x) { return x*x; }

inline int dcmp(double a,double b)
{
	if (fabs(a-b)<eps) return 0;
	if (a<b) return -1; return 1;
}

inline double Dist(Point A,Point B)
{
	return sqrt((A.X-B.X)*(A.X-B.X)+(A.Y-B.Y)*(A.Y-B.Y));
}

inline Point Get(Point a,Point b,Point c)
{
      double a1,a2,b1,b2,c1,c2;
      Point ans;
      a1=2*(b.X-a.X),b1=2*(b.Y-a.Y),c1=sqr(b.X)-sqr(a.X)+sqr(b.Y)-sqr(a.Y);
      a2=2*(c.X-a.X),b2=2*(c.Y-a.Y),c2=sqr(c.X)-sqr(a.X)+sqr(c.Y)-sqr(a.Y);
      if(!dcmp(a1,0)) 
      { 
           ans.Y=c1/b1; 
           ans.X=(c2-ans.Y*b2)/a2; 
      } 
      else if(!dcmp(b1,0)) 
      { 
           ans.X=c1/a1; 
           ans.Y=(c2-ans.X*a2)/b2; 
      } 
      else
      { 
          ans.X=(c2*b1-c1*b2)/(a2*b1-a1*b2); 
          ans.Y=(c2*a1-c1*a2)/(b2*a1-b1*a2); 
      } 
      return ans; 
}

inline Point Get(Point A,Point B)
{
	return Point((A.X+B.X)/2,(A.Y+B.Y)/2);
}

inline void MinCover()
{
	C=P[1]; R=0;
    for(int i=2;i<=n;i++)
    	if (dcmp(Dist(P[i],C),R)>0)
		{
			C=P[i];
			R=0;
        	for (int j=1;j<=i;j++)
				if (dcmp(Dist(P[j],C),R)>0)
				{
					C=Get(P[i],P[j]);
					R=Dist(C,P[i]);
            		for (int k=1;k<=j;k++)
						if (dcmp(Dist(P[k],C),R)>0)
						{
							C=Get(P[i],P[j],P[k]);
							R=Dist(C,P[i]);
						}
				}
		}
}

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%lf%lf",&P[i].X,&P[i].Y);
	MinCover();
	printf("%.10lf\n%.10lf %.10lf\n",R,C.X,C.Y);
	return 0;
}



你可能感兴趣的:([随机] 最小圆覆盖 BZOJ 2823 1337 1336)