Data structure (kd tree): HDU 4347 the Closest M Points

Source: Internet
Author: User
The Closest M Points

Time limit:16000/8000 MS (java/others) Memory limit:98304/98304 K (java/others)
Total submission (s): 3285 Accepted Submission (s): 1201


Problem DescriptionThe Course of software Design and development practice is objectionable. ZLC is facing a serious problem. There is many points in k-dimensional space. Given a point. ZLC need to find out the closest m points. Euclidean distance is used as the distance metric between and points. The Euclidean distance between points P and Q are the length of the line segment connecting them. In Cartesian coordinates, if p = (P1, p2,..., pn) and q = (Q1, Q2,..., QN) is the points in Euclidean N-space, then the D Istance from P to Q, or from Q to P are given by:

Can him solve this problem? Inputin the first line of the text file. There is non-negative integers n and K. They denote respectively:the number of points, 1 <= n <= 50000, and the number of dimensions,1 <= K <= 5. In each of the following n lines there are written k integers, representing the coordinates of a point. This followed by a line with one positive integer t, representing the number of queries,1 <= T <=10000.each Query Co Ntains, lines. The k integers in the first line represent the given point. In the second line, there are one integer m, the number of closest points you should find,1 <= m <=10. The absolute value of all the coordinates is not being more than 10000.
There is multiple test cases. Process to end of file. Outputfor each query, output m+1 lines:
The first line saying: "The closest m points was:" where M is the number of the points.
The following m lines representing m points, in accordance with the
It is guaranteed that the answer can are only being formed in one ways. The distances from the given point to all the nearest m+1 points is different. That means the input like this:
2 2
1 1
3 3
1
2 2
1
would not exist. Sample INPUT3 21 11 33 422 322 31 Sample outputthe closest 2 points are:1 4the closest 1 points are:1 3 rope hit the first kd tree, template problem, according to hit. ———— kd Tree = = pruning? Well, I'll think about it again.
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <queue>6 using namespacestd;7 Const intmaxn=200010;8 intcmpno,k;9 structnode{Ten     intx[Ten],l,r,id; One     BOOL operator< (ConstNode &b)Const{ A         returnx[cmpno]<B.x[cmpno]; -     } - }; the  - Long LongDis (ConstNode &a,ConstNode &b) { -     Long Longret=0; -      for(intI=0; i<k;i++) +ret+= (A.x[i]-b.x[i]) * (a.x[i]-b.x[i]); -     returnret;  + } A  at Node P[MAXN]; -  - intBuild (intLintRintd) { -     if(L>r)return 0; -cmpno=D; -     intMid=l+r>>1; inNth_element (p+l,p+mid,p+r+1); -P[mid].l=build (l,mid-1, (d+1)%K); toP[mid].r=build (mid+1, R, (d+1)%K); +     returnmid; - } the  *priority_queue<pair<Long Long,int> >Q; $ voidKth (intLintR,node Tar,intKintd) {Panax Notoginseng     if(L>r)return; -     intMid=l+r>>1; thepair<Long Long,int>v=Make_pair (Dis (P[mid],tar), p[mid].id); +     if(Q.size () ==k&&v<q.top ()) Q.pop (); A     if(Q.size () <k) Q.push (v); the     Long Longt=tar.x[d]-P[mid].x[d]; +     if(t<=0){ -Kth (l,mid-1, Tar,k, (d+1)%K); $         if(Q.top (). first>t*t) $Kth (mid+1, R,tar,k, (d+1)%K); -     } -     Else{ theKth (mid+1, R,tar,k, (d+1)%K); -         if(Q.top (). first>t*t)WuyiKth (l,mid-1, Tar,k, (d+1)%K); the     }     - } Wu intk,ans[ -]; - Node A[MAXN]; About intMain () { $     intN; -      while(SCANF ("%d%d", &n,&k)! =EOF) { -          for(intId=1; id<=n;id++){ -              for(intI=0; i<k;i++) Ascanf"%d",&p[id].x[i]); +P[id].id=ID; thea[id]=P[id];  -         } $Build (1N0); the         intQ; thescanf"%d",&Q); the Node tar; the          while(q--){ -              for(intI=0; i<k;i++) inscanf"%d",&tar.x[i]); thescanf"%d",&k); theKth (1, N,tar,k,0); Aboutprintf"The closest%d points are:\n", k); the             inttot=0; the              while(!Q.empty ()) { the                 intId=(Q.top ()). Second;q.pop (); +ans[tot++]=ID; -             } the              for(inti=tot-1; i>=0; i--)Bayi                  for(intj=0; j<k;j++) theprintf"%d%c", a[ans[i]].x[j],j==k-1?'\ n':' ');  the         } -     } -     return 0; the}

Data structure (kd tree): HDU 4347 the Closest M Points

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.