【BZOJ 3053】 The Closest M Points,bzoj3053

來源:互聯網
上載者:User

【BZOJ 3053】 The Closest M Points,bzoj3053
3053: The Closest M Points

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 376 Solved: 146
[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

kd-tree模板題。

kd-tree簡介

此題要求前k小答案,我們首先把k個inf加入小根堆,每次判斷是否比堆頂小來確定是否要加入答案。

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#define mp make_pair#define pa pair<int,int>#define M 500005#include <queue>#define inf 0x3f3f3f3f#define fi first#define se secondusing namespace std;priority_queue<pa> pq;int q[10],ans[M];struct node{    int ma[10],mi[10],d[10],l,r;}t[M];int root,n,m,k,now;void read(int &tmp){    tmp=0;    char ch=getchar();    int fu=1;    for (;ch<'0'||ch>'9';ch=getchar())        if (ch=='-') fu=-1;    for (;ch>='0'&&ch<='9';ch=getchar())        tmp=tmp*10+ch-'0';    tmp*=fu;}bool cmp(node a,node b){    return a.d[now]<b.d[now];}void Update(int x){    int l=t[x].l,r=t[x].r;    for (int i=0;i<k;i++)    {        t[x].mi[i]=min(t[x].mi[i],min(t[l].mi[i],t[r].mi[i]));        t[x].ma[i]=max(t[x].ma[i],max(t[l].ma[i],t[r].ma[i]));    }}int Build(int l,int r,int d){    now=d;    int mid=(l+r)>>1;    nth_element(t+1+l,t+1+mid,t+1+r,cmp);    if (mid!=l) t[mid].l=Build(l,mid-1,(d+1)%k);    else t[mid].l=0;    if (mid!=r) t[mid].r=Build(mid+1,r,(d+1)%k);    else t[mid].r=0;    Update(mid);    return mid;}int Sqr(int x){    return x*x;}int Dis(int x){    int ans=0;    for (int i=0;i<k;i++)        ans+=Sqr(t[x].d[i]-q[i]);    return ans;}int Get(int x){    int ans=0;    for (int i=0;i<k;i++)    {        if (q[i]<t[x].mi[i])            ans+=Sqr(t[x].mi[i]-q[i]);        if (q[i]>t[x].ma[i])            ans+=Sqr(q[i]-t[x].ma[i]);    }    return ans;}void Query(int x){    if (!x) return;    int d0=Dis(x),dl=Get(t[x].l),dr=Get(t[x].r);    if (d0<pq.top().fi)    {        pq.pop();        pq.push(mp(d0,x));    }    if (dl<dr)    {        if (dl<pq.top().fi)            Query(t[x].l);        if (dr<pq.top().fi)            Query(t[x].r);    }    else    {        if (dr<pq.top().fi)            Query(t[x].r);        if (dl<pq.top().fi)            Query(t[x].l);    }}int main(){    while (scanf("%d%d",&n,&k)!=EOF)    {        for (int j=0;j<k;j++)            t[0].ma[j]=-inf,t[0].mi[j]=inf;        for (int i=1;i<=n;i++)            for (int j=0;j<k;j++)            {                read(t[i].d[j]);                t[i].ma[j]=t[i].mi[j]=t[i].d[j];            }        root=Build(1,n,0);        int Q;        read(Q);        while (Q--)        {            for (int i=0;i<k;i++)                read(q[i]);            read(m);            printf("the closest %d points are:\n",m);            for (int i=1;i<=m;i++)                pq.push(mp(inf,0));            Query(root);            for (int i=1;i<=m;i++)                ans[i]=pq.top().se,pq.pop();            for (int i=m;i;i--)            {                for (int j=0;j<k;j++)                {                    if (j) printf(" ");                    printf("%d",t[ans[i]].d[j]);                }                printf("\n");            }        }    }    return 0; }

寫kd-tree的時候要注意:
1.查詢操作中if (!x) return;否則會RE

2.一開始要對t[0]的max和min賦值,使他們不可能成為更優的

3.插入操作中記得更新當前節點維護的極值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.