zoukankan      html  css  js  c++  java
  • 数据结构(KD树):HDU 4347 The Closest M Points

    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 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
      
      绳命中第一道KD树,模板题,照着打的。
      难道————KD树==剪枝?嗯,我再想想~~
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <queue>
     6 using namespace std;
     7 const int maxn=200010;
     8 int cmpNo,K;
     9 struct Node{
    10     int x[10],l,r,id;
    11     bool operator <(const Node &b)const{
    12         return x[cmpNo]<b.x[cmpNo];
    13     }
    14 };
    15 
    16 long long Dis(const Node &a,const Node &b){
    17     long long ret=0;
    18     for(int i=0;i<K;i++)
    19         ret+=(a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
    20     return ret;    
    21 }
    22 
    23 Node p[maxn];
    24 
    25 int Build(int l,int r,int d){
    26     if(l>r)return 0;
    27     cmpNo=d;
    28     int mid=l+r>>1;
    29     nth_element(p+l,p+mid,p+r+1);
    30     p[mid].l=Build(l,mid-1,(d+1)%K);
    31     p[mid].r=Build(mid+1,r,(d+1)%K);
    32     return mid;
    33 }
    34 
    35 priority_queue<pair<long long,int> >q;
    36 void Kth(int l,int r,Node tar,int k,int d){
    37     if(l>r)return;
    38     int mid=l+r>>1;
    39     pair<long long,int>v=make_pair(Dis(p[mid],tar),p[mid].id);
    40     if(q.size()==k&&v<q.top())q.pop();
    41     if(q.size()<k)q.push(v);
    42     long long t=tar.x[d]-p[mid].x[d]; 
    43     if(t<=0){
    44         Kth(l,mid-1,tar,k,(d+1)%K);
    45         if(q.top().first>t*t)
    46             Kth(mid+1,r,tar,k,(d+1)%K);
    47     }
    48     else{
    49         Kth(mid+1,r,tar,k,(d+1)%K);
    50         if(q.top().first>t*t)
    51             Kth(l,mid-1,tar,k,(d+1)%K);
    52     }    
    53 }
    54 int k,ans[20];
    55 Node a[maxn];
    56 int main(){
    57     int n;
    58     while(scanf("%d%d",&n,&K)!=EOF){
    59         for(int id=1;id<=n;id++){
    60             for(int i=0;i<K;i++)
    61                 scanf("%d",&p[id].x[i]);
    62             p[id].id=id;
    63             a[id]=p[id];    
    64         }
    65         Build(1,n,0);
    66         int Q,tot;
    67         scanf("%d",&Q);
    68         Node tar;
    69         while(Q--){
    70             for(int i=0;i<K;i++)
    71                 scanf("%d",&tar.x[i]);
    72             scanf("%d",&k);
    73             printf("the closest %d points are:
    ",k);
    74             for(int i=1;i<=k;i++)q.push(make_pair(1e18,-1));
    75             Kth(1,n,tar,k,0);tot=0;
    76             while(!q.empty()){
    77                 int id=(q.top()).second;q.pop();
    78                 ans[tot++]=id;
    79             }
    80             for(int i=tot-1;i>=0;i--)
    81                 for(int j=0;j<K;j++)
    82                     printf("%d%c",a[ans[i]].x[j],j==K-1?'
    ':' ');        
    83         }
    84     }
    85     return 0;
    86 }
     
    尽最大的努力,做最好的自己!
  • 相关阅读:
    shell 指令
    在Linux下搭建nRF51822的开发烧写环境(makefile版)
    宏定义。字符串拼接和字符串整形转字符串
    django-debug-toolbar安装过程中的error
    pipenv
    Docker 命令大全
    MySQL性能优化
    docker操作
    使用网易源解决docker下载镜像文件慢的问题
    w3school/jQuery 教程
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5574384.html
Copyright © 2011-2022 走看看