• hdu 1007 分治法


    http://acm.hdu.edu.cn/showproblem.php?pid=1007

     好费劲的一题,但还是被我A了,学到不少东西,骚年继续加油吧!

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <math.h>
    using namespace std;
    struct coor
    {
         double x,y;
    }p[100005];
    
    int a[100005];
    int cmpx(const coor &a,const coor &b)
    {
        return a.x<b.x;
    }
    
    int cmpy(const int &a,const int &b)
    {
         return p[a].y<p[b].y;
    }
    
    inline double dist(coor &a,coor &b)
    {
         return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    
    inline double min(double a,double b)
    {
        return a<b?a:b;
    }
    double merger(int low,int high)
    {
         if(low+1==high)
            return dist(p[low],p[high]);
         if(low+2==high)
            return min(dist(p[low],p[high]),min(dist(p[low],p[low+1]),dist(p[low+1],p[high])));
         int mid=(low+high)>>1;
         double ans=min(merger(low,mid),merger(mid+1,high));
         int i,j,cnt=0;
         for(i=low;i<=high;++i)
         {
              if(p[i].x>=p[mid].x-ans&&p[i].x<=p[mid].x+ans)
              a[cnt++]=i;
         }
         sort(a,a+cnt,cmpy);
         for(i=0;i<cnt;++i)
         {
              for(j=i+1;j<cnt;++j)
              {
                  if(p[a[j]].y-p[a[i]].y>=ans)
                  break;
                  ans=min(ans,dist(p[a[i]],p[a[j]]));
              }
         }
         return ans;
    
    }
    int main()
    {
          int n,m,i;
          while(scanf("%d",&n)!=EOF&&n!=0)
          {
                for(i=0;i<n;++i)
                {
                      scanf("%lf%lf",&p[i].x,&p[i].y);
    
                }
                sort(p,p+n,cmpx);
                printf("%.2lf
    ",merger(0,n-1)/2);
          }
          return 0;
    }
  • 相关阅读:
    接口与实现分离
    C++的explicit关键字
    C++的类型转换
    使用catch做单元测试简介
    C++可调用对象与函数表
    在sublime中使用cppcheck
    你需要的代码静态检查
    构造析构与拷贝赋值那些事
    c++的关联容器入门(map and set)
    【致敬程序猿】
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3432376.html
Copyright © 2020-2023  润新知