[BZOJ 3053] The Closest M Points, bzoj3053

Source: Internet
Author: 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 using points in K-dimen1_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:
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?

Soft Institute of Technology courses are annoying! Comrade ZLC encountered a headache: there are many points in the K-dimensional space. For some given points, ZLC needs to find m points closest to it.

(The distance here refers to the Euclidean distance: D (p, q) = D (q, p) = sqrt (q1-p1) ^ 2 + (q2-p2) ^ 2 + (q3-p3) ^ 2 +... + (Qn-pn) ^ 2)

ZLC is going to fight Dota, so you have to help solve it ......

[Input]

The first line has two non-negative integers: Point n (1 <= n <= 50000), and dimension k (1 <= k <= 5 ).
In the next n rows, each line contains k integers, representing the coordinates of a point.
The next positive integer: The given number of queries t (1 <= t <= 10000)
The following 2 * t lines:
The first line, k integers: coordinates of the given point
Row 2: query the nearest m points (1 <= m <= 10)

The absolute values of all coordinates cannot exceed 10000.
There are multiple groups of data!

[Output]

For each query, output m + 1 line:
The first line: "the closest m points are:" m is m in the query
Next, each row in the m line represents a vertex, sorted from near to far.

Ensure that the solution is unique. The following situations do not occur:
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 shoshould 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 template question.

Kd-tree

Before this question is required K The answer is as follows: K Items Inf Add a small root heap, and check whether the heap is smaller than the heap top to determine whether to add the answer.

#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; }

When writing kd-treeNote::
1. query operation in progressif (!x) return;Otherwise, RE

2. T [0] Of Max And Min Assign values, making them impossible to be better

3. In the insert operation, remember to update the extreme values maintained by the current node.

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.