Refer to an algorithm on the internet, but always is the mle, really can not think out what method.
Reference Blog: The beauty of programming find the nearest point
Importjava.util.Arrays;ImportJava.util.Scanner; Public classMain { Public Static intn=100000; Public Static int[] list=New int[N]; Public StaticPoint[] Ptns=NewPoint[n]; Public Static floatmax=Float.max_value; Public Static floatDisintIintj) {return(ptns[i].x-ptns[j].x) * (ptns[i].x-ptns[j].x) + (PTNS[I].Y-PTNS[J].Y) * (ptns[i].y-ptns[j].y); } Public Static floatClosestpair (intLeftintRight ) { floatD=MAX; if(left==Right )returnD; Else if(left+1==Right )returndis (left, right); intMid= (left+right) >>1; floatd1=Closestpair (left, mid); floatD2=closestpair (mid+1, right); D=math.min (D1, D2); intI=0,j=0,k=0; for(i=left;i<=right;i++) { if(Math.Abs (PTNS[MID].Y-PTNS[I].Y) <d && i!=mid) List[k++]=i; } //find the nearest point to the point in D distance for(i=0;i<k;i++) { for(J=i+1;j<k && Math.Abs (ptns[list[j]].x-ptns[list[i]].x) <d;j++) { floatdx=dis (list[i], list[j]); if(d>dx) {d=DX; } } } returnD; } Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); intn=Integer.max_value; N=Scanner.nextint (); while(n!=0) { for(inti=0;i<n;i++) { point point=NewPoint (); Point.x=scanner.nextfloat (); Point.y=scanner.nextfloat (); Ptns[i]=Point ; } arrays.sort (Ptns,0,n-1); System.out.println (String.Format ("%.2f", Math.sqrt (Closestpair (0, n-1))/2)); N=Scanner.nextint (); } scanner.close (); } Static classPointImplementsComparable<point> { Public floatx; Public floaty; @Override Public intcompareTo (Point O) {//TODO auto-generated Method Stub if( This.y>o.y)return1; Else if( This.y<o.y)return-1; Else { if( This.x>o.x)return1; Else if( This.x<o.x)return-1; return0; } } }}
Hdoj 1007 Java MLE Pass but