HDU 1007 Quoit Design

题解:最近点对模板

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef double D;
struct Q{D x,y;}q[100001],sl[10],sr[10];
int cntl,cntr,lm,rm; D ans;
int cmp(Q p1,Q p2){return(p1.x>p2.x);}
D Cal(D x1,D y1,D x2,D y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
void Mindis(int l, int r){
    if(l==r)return; D dis;
    if (l+1==r){
        dis=Cal(q[l].x,q[l].y,q[r].x,q[r].y);
        if(ans>dis)ans=dis; return;
    }
    int mid=(l+r)>>1,i,j;
    Mindis(l,mid); Mindis(mid+1,r);
    lm=mid+1-5; if(lm<l)lm=l; 
    rm=mid+5; if(rm>r)rm=r; 
    cntl=cntr=0;
    for(i=mid;i>=lm;i--){
        if(q[mid+1].x-q[i].x>=ans)break;
        sl[++cntl]=q[i];
    }
    for(i=mid+1;i<=rm;i++){
        if (q[i].x-q[mid].x>=ans)break;
        sr[++cntr]=q[i];
    }
    for(i=1;i<=cntl;i++)
        for(j=1;j<=cntr;j++){
            dis=Cal(sl[i].x,sl[i].y,sr[j].x,sr[j].y);
            if(dis<ans)ans=dis;
        }
}
int main(){
    int n,i;
    while (scanf("%d",&n)==1&&n){
        for(i=1;i<=n;i++)scanf("%lf%lf",&q[i].x,&q[i].y);
        sort(q+1,q+n,cmp);
        ans=Cal(q[1].x,q[1].y,q[2].x,q[2].y);
        Mindis(1,n); printf("%.2lf\n",ans/2.0);
    }
    return 0;
}
posted @ 2014-08-29 14:49  forever97  阅读(165)  评论(0编辑  收藏  举报