[bzoj3053][kd樹]The Closest M Points

來源:互聯網
上載者:User

3053: The Closest M Points

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1035 Solved: 363
[Submit][Status][Discuss]
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:
D(p,q)=D(q,p)=sqrt((q1-p1)^2+(q2-p2)^2+(q3-p3)^2…+(qn-pn)^2
Can you help him solve this problem?

軟工學院的課程很討厭。ZLC同志遇到了一個頭疼的問題:在K維空間裡面有許多的點,對於某些給定的點,ZLC需要找到和它最近的m個點。

(這裡的距離指的是歐幾裡得距離:D(p, q) = D(q, p) = sqrt((q1 - p1) ^ 2 + (q2 - p2) ^ 2 + (q3 - p3) ^ 2 + … + (qn - pn) ^ 2)

ZLC要去打Dota,所以就麻煩你幫忙解決一下了……

【Input】

第一行,兩個非負整數:點數n(1 <= n <= 50000),和維度數k(1 <= k <= 5)。
接下來的n行,每行k個整數,代表一個點的座標。
接下來一個正整數:給定的詢問數量t(1 <= t <= 10000)
下面2*t行:
  第一行,k個整數:給定點的座標
  第二行:查詢最近的m個點(1 <= m <= 10)

所有座標的絕對值不超過10000。
有多組資料。

【Output】

對於每個詢問,輸出m+1行:
第一行:”the closest m points are:” m為查詢中的m
接下來m行每行代表一個點,按照從近到遠排序。

保證方案唯一,下面這種情況不會出現:
2 2
1 1
3 3
1
2 2
1

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

HINT

Source

k-d tree

sol:
對,我們看到source裡面寫了kd tree,所以我們很自然寫起了kdtree。用一個堆維護一下已經存起來的點就行了,然後裸暴力。

#include<algorithm>#include<iostream>#include<string>#include<cstring>#include<cmath>#include<cstdio>#include<cstdlib>#include<queue>using namespace std;int n,K,m;int read(){    char c;    bool flag=false;    while((c=getchar())>'9'||c<'0')    if(c=='-')flag=true;    int res=c-'0';    while((c=getchar())>='0'&&c<='9')    res=(res<<3)+(res<<1)+c-'0';    return flag?-res:res;}const int N=51000;int l[N],r[N];struct cc{    int x[6],min[6],max[6];}a[N],tr[N];struct dd{    int dis,name;    friend bool operator <(dd a,dd b)    {        return a.dis<b.dis;    }};priority_queue<dd> q;int f;bool cmp(cc k,cc P){    return P.x[f]>k.x[f];}void build(int &k,int flag,int L,int R){    if(flag>=K) flag-=k;    k=L+R>>1;    f=flag;    nth_element(a+L,a+k,a+R+1,cmp);    tr[k]=a[k];    if(L<=k-1) build(l[k],flag+1,L,k-1);    if(k+1<=R) build(r[k],flag+1,k+1,R);    for(int i=0;i<K;++i)    tr[k].min[i]=min(tr[k].x[i],min(tr[l[k]].min[i],tr[r[k]].min[i])),    tr[k].max[i]=max(tr[k].x[i],max(tr[l[k]].max[i],tr[r[k]].max[i]));}void insert(int &k,int flag,cc P){    if(!k)    {        k=++n;        tr[k]=P;        return;    }    if(flag>=K) flag-=k;    f=flag;    int d=cmp(tr[k],P);    if(d) d=r[k];    else  d=l[k];    insert(d,flag+1,P);    for(int i=0;i<K;++i)    tr[k].min[i]=min(tr[k].min[i],tr[d].min[i]),    tr[k].max[i]=max(tr[k].max[i],tr[d].max[i]);}const int inf=1e9;int sqr(int x){    return x*x;}int pre_dis(cc k,cc P){    int res=0;    int c;    for(int i=0;i<K;++i)    {        c=0;        c+=max(k.min[i]-P.x[i],0);        c+=max(P.x[i]-k.max[i],0);        res+=sqr(c);    }    return res;}int dis(cc a,cc b){    int res=0;    for(int i=0;i<K;++i)    res+=sqr(a.x[i]-b.x[i]);    return res;}int num;void query(int k,int flag,cc P){    int fx,fy,p;    fx=fy=inf;    p=dis(tr[k],P);    if(q.size()<num||q.top().dis>p)     {        if(q.size()==num) q.pop();        q.push((dd){p,k});    }    if(flag>=K) flag-=K;    if(l[k]) fx=pre_dis(tr[l[k]],P);    if(r[k]) fy=pre_dis(tr[r[k]],P);    if(fx<fy)    {        if(q.top().dis>fx||q.size()<num) query(l[k],flag+1,P);        if(q.top().dis>fy||q.size()<num) query(r[k],flag+1,P);    }    else    {        if(q.top().dis>fy||q.size()<num) query(r[k],flag+1,P);        if(q.top().dis>fx||q.size()<num) query(l[k],flag+1,P);    }}int root;int b[N];int main(){//  freopen("3053.txt","r",stdin);//  freopen("3053out.txt","w",stdout);    for(int i=0;i<6;++i) tr[0].min[i]=inf,tr[0].max[i]=-inf;//  n=read();K=read();    while(~scanf("%d%d",&n,&K))    {        root=0;        for(int i=1;i<=n;++i)        for(int j=0;j<K;++j)            a[i].x[j]=read();        build(root,0,1,n); //      for(int i=1;i<=n;++i)//      printf("%d %d %d %d %d %d\n",tr[i].x[0],tr[i].x[1],tr[i].min[0],tr[i].min[1],tr[i].max[0],tr[i].max[1]);        m=read();        cc P;        int name;        for(int i=1;i<=m;++i)        {            while(!q.empty()) q.pop();            for(int j=0;j<K;++j) P.x[j]=read();            num=read();            query(root,0,P);            printf("the closest %d points are:\n",num);            for(int j=1;j<=num;++j)            b[j]=q.top().name,q.pop();            for(int j=num;j>=1;--j)            {                for(int l=0;l<K;++l)                {                    printf("%d",tr[b[j]].x[l]);                    if(l<K-1)printf(" ");                }                printf("\n");            }        }        for(int i=1;i<=n;++i)        l[i]=r[i]=0;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.