Quoit Design ring toss game (HDU1007)

Quoit Design

Understand the meaning of problems discovered after the plane is to find the distance between the closest point divided by two.

Closest point on the plane is a classic divide and conquer, my resolve

Directly on the code

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1000005;
const double inf=1e12;
struct node{
    double x,y;
}p[N];
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
bool cmp2(int a,int b)
{
    return p[a].y<p[b].y;
}
double dist(int a,int b){return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));}
int n,a[N];
double msort(int l,int r)
{
    double d=inf;
    if(l==r)return d;
    if(l==r-1)return dist(l,r);
    int mid=l+r>>1;
    double d1=msort(l,mid);
    double d2=msort(mid+1,r);
    d=min(d1,d2);
    int cnt=0;
    for(int i=l;i<=r;i++)
        if(fabs(p[i].x-p[mid].x)<=d)a[++cnt]=i;
    sort(a+1,a+cnt+1,cmp2);
    for(int i=1;i<=cnt;i++)
    {
        for(int j=i+1;j<=cnt&&fabs(p[a[i]].y-p[a[j]].y)<d;j++)
        {
            d=min(d,dist(a[i],a[j]));
        }
    }
    return d;
}
signed main()
{
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==0)return 0;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p+1,p+n+1,cmp);
        printf("%.2lf\n",msort(1,n)/2);
    }
}

Guess you like

Origin www.cnblogs.com/zzctommy/p/12350042.html