POJ 3714 nearest point to

Source: Internet
Author: User

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&LT;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", &AMP;P[I].X,&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.