百练+最近邻点对+递归分治法的运用+计算在左右两边的点距离部分最精彩

点击打开链接
//  main.cpp
//  test
//
//  Created by 吴有堃 on 2017/9/11.
//  Copyright © 2017年 吴有堃. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define mod 1e9+7
using namespace std;
const int maxn=2e3+5;
struct Point{
    double x,y;
    int flag;
}p[maxn];
int arr[maxn];
double dis(Point a, Point b)
{
    return  sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp_x(Point a, Point b)
{
    return a.x<b.x;
}
bool cmp_y(int a, int b)
{
    return p[a].y<p[b].y;
}
double solve(int l, int r)
{
    if(r==l+1){
        if(p[l].flag!=p[r].flag){
            return dis(p[l],p[r]);
        }
        else return inf;
    }
    else if(r==l+2){
        if(p[l].flag==p[l+1].flag&&p[l+1].flag==p[l+2].flag&&p[l].flag==p[l+2].flag){//三个都相等
            return inf;
        }
        else if(p[l].flag==p[l+1].flag){//两两相等
            return  min(dis(p[l],p[l+2]),dis(p[l+1],p[l+2]));
        }
        else if(p[l].flag==p[l+2].flag){
            return min(dis(p[l],p[l+1]),dis(p[l+2],p[l+1]));
        }
        else if(p[l+1].flag==p[l+2].flag){
            return min(dis(p[l+1],p[l]),dis(p[l+2],p[l]));
        }
        else return min(dis(p[l],p[l+1]), min(dis(p[l],p[l+2]),dis(p[l+1],p[l+2])));//互不相等
    }
    int mid=(l+r)/2;
    double ans=min(solve(l,mid),solve(mid+1,r));
    int i=0,j=0,cnt=0;
    for(i=l;i<=r;i++){
        if( p[i].x>=p[mid].x-ans && p[i].x<=p[mid].x+ans ){
            arr[cnt++]=i;
        }
    }
    sort(arr,arr+cnt,cmp_y);
    for(i=0;i<cnt;i++){
        for(j=i+1;j<cnt;j++){
            if(p[arr[j]].flag!=p[arr[i]].flag){
                if(p[arr[j]].y-p[arr[i]].y>ans) break;//点开始不满足了
                ans=min(ans,dis(p[arr[j]],p[arr[i]]));
            }
        }
    }
    return ans;
}
int main()
{
    int i=0,T=0,N=0;
    scanf("%d",&T);
    while (T--) {
        scanf("%d",&N);
        for(i=0;i<N;i++){
            scanf("%lf %lf",&p[i].x,&p[i].y);
            p[i].flag=1;
        }
        for(i=N;i<N+N;i++){
            scanf("%lf %lf",&p[i].x,&p[i].y);
            p[i].flag=2;
        }
        sort(p,p+N+N,cmp_x);
        printf("%.3f\n",solve(0,N+N-1));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值