【BZOJ 3053】 The Closest M Points

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简介

此题要求前加入小根堆,每次判断是否比堆顶小来确定是否要加入答案。

using 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){;}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.一开始要对赋值,使他们不可能成为更优的

3.插入操作中记得更新当前节点维护的极值

不知道来年,会不会开出一地的记忆和忧愁。

【BZOJ 3053】 The Closest M Points

相关文章:

你感兴趣的文章:

标签云: