The Closest M Points

The Closest M Points

http://acm.hdu.edu.cn/showproblem.php?pid=4347

 

参考博客:https://blog.csdn.net/acdreamers/article/details/44664645#commentBox

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 7461    Accepted Submission(s): 2335


Problem Description
The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem .There are 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 two points. The Euclidean distance between points p and q is the length of the line segment connecting them.In Cartesian coordinates, if p = (p1, p2,..., pn) and q = (q1, q2,..., qn) are two points in Euclidean n-space, then the distance from p to q, or from q to p is given by:

Can you help him solve this problem?
 
Input
In the first line of the text file .there are two 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 is 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 contains two lines. The k integers in the first line represent the given point. In the second line, there is one integer m, the number of closest points you should find,1 <= m <=10. The absolute value of all the coordinates will not be more than 10000.
There are multiple test cases. Process to end of file.
 
Output
For each query, output m+1 lines:
The first line saying :”the closest m points are:” where m is the number of the points.
The following m lines representing m points ,in accordance with the order from near to far
It is guaranteed that the answer can only be formed in one ways. The distances from the given point to all the nearest m+1 points are different. That means input like this:
2 2
1 1
3 3
1
2 2
1
will not exist.
 
 
Sample Input
3 2
1 1
1 3
3 4
2
2 3
2
2 3
1
 

 

Sample Output
the closest 2 points are:
1 3
3 4
the closest 1 points are:
1 3
 
Author
HIT
 
Source
 
模板题
  1 #include<iostream>
  2 #include<queue>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cstdio>
  6 #define N 50005
  7 using namespace std;
  8 
  9 int n,m,id;//n是点数,m是维度,id是当前切的维度
 10 
 11 struct sair{
 12     int p[5];
 13     bool operator<(const sair &b)const{
 14         return p[id]<b.p[id];
 15     }
 16 }_data[N],data[N<<3];
 17 int flag[N<<3];
 18 
 19 priority_queue<pair<double,sair> >Q;
 20 
 21 void build(int l,int r,int rt,int dep){
 22     if(l>r) return;
 23     flag[rt]=1;
 24     flag[rt<<1]=flag[rt<<1|1]=-1;
 25     id=dep%m;
 26     int mid=l+r>>1;
 27     nth_element(_data+l,_data+mid,_data+r+1);
 28     data[rt]=_data[mid];
 29   //  if(l==r) return;
 30     build(l,mid-1,rt<<1,dep+1);
 31     build(mid+1,r,rt<<1|1,dep+1);
 32 }
 33 
 34 void query(sair p,int k,int rt,int dep){
 35     if(flag[rt]==-1) return;
 36     pair<double,sair> cur(0,data[rt]);//获得当前节点
 37     for(int i=0;i<m;i++){//计算当前节点到P点的距离
 38         cur.first+=(cur.second.p[i]-p.p[i])*(cur.second.p[i]-p.p[i]);
 39     }
 40     int idx=dep%m;
 41     int fg=0;
 42     int x=rt<<1;
 43     int y=rt<<1|1;
 44     if(p.p[idx]>=data[rt].p[idx]) swap(x,y);
 45     if(~flag[x]) query(p,k,x,dep+1);
 46     //开始回溯
 47     if(Q.size()<k){
 48         Q.push(cur);
 49         fg=1;
 50     }
 51     else{
 52         if(cur.first<Q.top().first){
 53             Q.pop();
 54             Q.push(cur);
 55         }
 56         if((p.p[idx]-data[rt].p[idx])*(p.p[idx]-data[rt].p[idx])<Q.top().first){
 57             fg=1;
 58         }
 59     }
 60     if(~flag[y]&&fg){
 61         query(p,k,y,dep+1);
 62     }
 63 }
 64 
 65 sair ans[15];
 66 
 67 int main(){
 68     while(~scanf("%d %d",&n,&m)){
 69         for(int i=1;i<=n;i++){
 70             for(int j=0;j<m;j++){
 71                 scanf("%d",&_data[i].p[j]);
 72             }
 73         }
 74         build(1,n,1,0);
 75         int t,k;
 76         scanf("%d",&t);
 77         while(t--){
 78             sair tmp;
 79             for(int i=0;i<m;i++){
 80                 scanf("%d",&tmp.p[i]);
 81             }
 82             scanf("%d",&k);
 83             while(!Q.empty()){
 84                 Q.pop();
 85             }
 86             printf("the closest %d points are:\n",k);
 87             query(tmp,k,1,0);
 88             for(int i=0;i<k;i++){
 89                 ans[i]=Q.top().second;
 90                 Q.pop();
 91             }
 92             for(int i=k-1;i>=0;i--){
 93                 for(int j=0;j<m;j++){
 94                     if(!j) printf("%d",ans[i].p[j]);
 95                     else printf(" %d",ans[i].p[j]);
 96                 }
 97                 printf("\n");
 98             }
 99         }
100     }
101 }
View Code

 

posted on 2018-10-29 19:50  Fighting_sh  阅读(192)  评论(0编辑  收藏  举报

导航