The plane of the closest point

Title Description

Gives the n points in the same plane, the closest point is obtained from

Portal

answer

Here not to discuss the essence of the kind of human practices: random rotation

Consider the partition of
the sequence of the entire point of press x first row again order
if you want to divide and conquer, apparently to deal with separate certain part in the merger
after we directly consider half, according to the x-coordinate-half, respectively, obtained recently from two parts and then merge

Then how do we merge
in the merging process, we have to consider, there may be just on the point of separation across our two mid circumstances
so we add a bit to find the limit violence

Code

#include<bits/stdc++.h>
using namespace std;
#define re register
#define in inline
#define ll long long
#define db double
#define get getchar()
in int read()
{
    int t=0; char ch=get;
    while(ch<'0' || ch>'9') ch=get;
    while(ch<='9' && ch>='0') t=t*10+ch-'0', ch=get;
    return t;
}
const int _=3e5+9; 
int n;
struct yzx{
    db y,x;
}a[_];
in db dis(yzx a,yzx b)
{
    return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(b.y-a.y)*abs(b.y-a.y));
}
in int cmp1(int x,int y)
{
    return a[x].y<a[y].y;
}
int num[_];  
in db work(int l,int r)
{
    db  ans=1000000009;
    if(l==r)
        return ans;
    if(l+1==r)
        return dis(a[l],a[r]);
    int mid=l+r>>1;
    ans=min(work(l,mid),work(mid+1,r));
    int cnt=0;
    for(re int i=l;i<=r;i++)
    if(abs(a[i].x-a[mid].x)<=ans) num[++cnt]=i;
    sort(num+1,num+cnt+1,cmp1);
    for(re int i=1;i<cnt;i++)
        for(re int j=i+1;j<=cnt&&abs(a[num[j]].y-a[num[i]].y)<ans;j++)
            ans=min(ans,dis(a[num[i]],a[num[j]]));
    return ans;
}
in int cmp(yzx x,yzx y) {
    if (x.x == y.x)
        return x.y < y.y;
    else
        return x.x < y.x;
}
int main()
{
    n=read();
    for(re int i=1;i<=n;i++)
        a[i].x=read(),a[i].y=read();
    sort(a+1,a+n+1,cmp);
    printf("%.4lf\n",work(1,n));
}

Guess you like

Origin www.cnblogs.com/yzhx/p/11706686.html