POJ 3714 nearest point to
Test instructions
Given n A-class points, N-class-B points, the nearest distance from a-class point to a Class B point is obtained.
Limit:
1 <= N <= 1e5
0 <= x, y <= 1e9
Ideas:
Point Division Treatment
/*POJ 3714 Test Instructions: gives the N Class A point, n a Class B point, the nearest distance from the class A point to the Class B point. Limit: 1 <= n <= 1e5 0 <= x, y <= 1e9 Ideas: Point Division */#include <iostream> #include <cstdio> #include <algo Rithm> #include <cmath>using namespace Std;const double inf=1e30;struct point{double x,y;int s; Point () {}point (double _x,double _y) {x=_x; y=_y;} Point operator – (point P) {return point (X-P.X,Y-P.Y);} Double dot (point P) {return x*p.x+y*p.y;}}; const int n=1e5+5; Point P[2*n];int t[2*n];bool Cmpxy (point A,point B) {if (a.x==b.x) return A.y<b.y;return a.x<b.x;} BOOL Cmpy (int a,int b) {return p[a].y<p[b].y;} Double Dist (point A,point b) {return sqrt ((a). dot (a));} Double Gao (int l,int r) {double ret=inf;if (l==r) return ret;if (l+1==r) {if (P[L].S!=P[R].S) return dist (P[l],p[r]); else return ret;} int mid= (L+R) >>1;double d=min (Gao (l,mid), Gao (mid+1,r)), int cnt=0;for (int i=l;i<r;++i) {if (Fabs (p[mid].x-p[ i].x) <d) t[cnt++]=i;} Sort (t,t+cnt,cmpy); for (int i=0;i<cnt;++i) for (int j=i+1;j<cnt && P[T[J]].Y-P[T[I]].Y<D;++J) if (P[T[I]].S!=P[T[J]].S) d=min (D,dist (P[t[i]],p[t[j]]); return D;} int main () {int t;scanf ("%d", &t), int n;while (t--) {scanf ("%d", &n), for (int i=0;i<n;++i) {scanf ("%lf%lf", &P[I].X,&P[I].Y);p [I].s=0;} for (int i=n;i<2*n;++i) {scanf ("%lf%lf", &p[i].x,&p[i].y);p [I].s=1;} Sort (p,p+2*n,cmpxy);p rintf ("%.3f\n", Gao (0,2*n-1));} return 0;}
POJ 3714 nearest point to