最近点对

来源:互联网 发布:关闭windows数字签名 编辑:程序博客网 时间:2024/05/14 21:38
    #include<iostream>      #include<cstdio>      #include<string>      #include<cstring>      #include<vector>      #include<cmath>      #include<queue>      #include<stack>      #include<map>      #include<set>      #include<algorithm>      using namespace std;      const int maxn=100010;      const double INF=10000000000000.0;      const int eps=1e-6;      int N;      double mind;      struct node      {          double x,y;          bool operator<(const node &a)const          {              if(fabs(x-a.x)<eps)return y<a.y;              return x<a.x;          }      }p[maxn],tmp[maxn];      bool cmp(node a,node b)      {          return a.y<b.y;      }      double cal(node a,node b)      {          double x=a.x-b.x;          double y=a.y-b.y;          return sqrt(x*x+y*y);      }      double solve(int l,int r)      {          if(l==r)return INF;          if(l+1==r)return cal(p[l],p[r]);          int mid=(l+r)>>1;          double d=INF;          d=min(d,solve(l,mid-1));          d=min(d,solve(mid+1,r));          int k=0;          for(int i=l;i<=r;i++)              if(fabs(p[mid].x-p[i].x)<=d)tmp[k++]=p[i];          sort(tmp,tmp+k,cmp);          for(int i=0;i<k;i++)          {              for(int j=i+1;j<k&&fabs(tmp[j].y-tmp[i].y)<=d;j++)              {                  double dis=cal(tmp[i],tmp[j]);                  if(dis<d)                      d=dis;              }          }          return d;      }      int main()      {//freopen("in.txt","r",stdin);          while(scanf("%d",&N)!=EOF)          {              for(int i=1;i<=N;i++)scanf("%lf%lf",&p[i].x,&p[i].y);              sort(p+1,p+1+N);              double ans=solve(1,N);              printf("%.2lf\n",ans/2);          }          return 0;      }  

0 0
原创粉丝点击