Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)

Total Submission(s): 3520    Accepted Submission(s): 1247

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
 
Author
HIT
 
Source
 

【题解】

kd-tree,虽然不是曼哈顿距离。但是还是能够从曼哈顿距离里面得到启发的。

本来的估价函数的原则是

如果在矩形内就返回0.这点不变。

然后在矩形外就返回到这个矩形的最小曼哈顿距离。现在改成欧几里得距离就可以了。

然后返回这个距离作为估价函数。

取估价函数较小的那个方向更新解就可以了。

前k小的话可以加一个队列维护。因为k最大为10,所以没必要写二分。

具体的看代码吧。

着重看一下估价函数就好。

//数据范围实际上是可以不用定义long long的。但是保险起见。

//k维的并没有什么可怕的。。就是for 0->1变成for 0->k-1

//然后要注意多个维的点的输出最后一个维的坐标后面不能多空格。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int MAXN = 60000;
const int MAXK_TH = 20;
const long long INF = 4410000000000000000; struct point
{
long long d[5], mi_n[5], ma_x[5];
int l, r;
}; struct data2
{
long long dis, d[5];
}; int n, k, root, now, k_th;
point t[MAXN], p[MAXN], op;
long long ans;
data2 dl[MAXK_TH]; bool cmp(point a, point b)
{
if (a.d[now] < b.d[now])
return true;
return false;
} void push_up(int rt)
{
int l = t[rt].l, r = t[rt].r;
for (int i = 0; i <= k - 1; i++)
{
if (l)
{
t[rt].ma_x[i] = max(t[l].ma_x[i], t[rt].ma_x[i]);
t[rt].mi_n[i] = min(t[l].mi_n[i], t[rt].mi_n[i]);
}
if (r)
{
t[rt].ma_x[i] = max(t[r].ma_x[i], t[rt].ma_x[i]);
t[rt].mi_n[i] = min(t[r].mi_n[i], t[rt].mi_n[i]);
}
}
} int build(int begin, int end, int fx)
{
int m = (begin + end) >> 1;
now = fx;
nth_element(p + begin, p + m, p + end + 1, cmp);
t[m] = p[m];
for (int i = 0; i <= k - 1; i++)
t[m].ma_x[i] = t[m].mi_n[i] = t[m].d[i];
if (begin < m)
t[m].l = build(begin, m - 1, (fx + 1) % k);
if (m < end)
t[m].r = build(m + 1, end, (fx + 1) % k);
push_up(m);
return m;
} void input_data()
{
memset(t, 0, sizeof(t));
for (int i = 1; i <= n; i++)
for (int j = 0; j <= k - 1; j++)
scanf("%lld", &p[i].d[j]);
root = build(1, n, 0);
} long long get_dis(point a, point b)
{
long long temp = 0;
for (int i = 0; i <= k - 1; i++)
temp += (a.d[i] - b.d[i])*(a.d[i] - b.d[i]);
return temp;
} long long sqr(long long x)
{
return x*x;
} long long gujia_min(int rt) //某个子树的估价最小函数
{
long long temp = 0;
for (int i = 0; i <= k - 1; i++)
{
//min(sqr(t[rt].ma_x[i] - op.d[i]), sqr(t[rt].mi_n[i] - op.d[i]));
long long temp1 = op.d[i] - t[rt].ma_x[i];
long long temp2 = t[rt].mi_n[i] - op.d[i];
if (temp1 > 0)
temp += sqr(op.d[i] - t[rt].ma_x[i]);
if (temp2 > 0)
temp += sqr(t[rt].mi_n[i] - op.d[i]);
}
return temp;
} void query_min(int rt)
{
long long dis = get_dis(t[rt], op);
int tempk = k_th;
while (dl[tempk].dis > dis)
{
tempk--;
if (!tempk)
break;
}
if (tempk != k_th) //如果比第k小的还小 就往前找一个合适的位置放进去。
{//然后第k小的就被挤掉 更新了。
for (int i = k_th; i >= tempk + 2; i--)
dl[i] = dl[i - 1];
dl[tempk + 1].dis = dis;
for (int i = 0; i <= k - 1; i++)
dl[tempk + 1].d[i] = t[rt].d[i];
}
long long gl = INF, gr = INF;
int l = t[rt].l, r = t[rt].r;
if (l)
gl = gujia_min(l);
if (r)
gr = gujia_min(r);
if (gl < gr)
{
if (gl < dl[k_th].dis)
query_min(l);
if (gr < dl[k_th].dis)
query_min(r);
}
else
{
if (gr < dl[k_th].dis)
query_min(r);
if (gl < dl[k_th].dis)
query_min(l);
}
} void output_ans()
{
int t;
scanf("%d", &t);
while (t--)
{
for (int i = 0; i <= k - 1; i++)
scanf("%lld", &op.d[i]);
scanf("%d", &k_th);
for (int i = 0; i <= k_th; i++)
dl[i].dis = INF;
ans = INF;
query_min(root);
printf("the closest %d points are:\n", k_th);
for (int i = 1; i <= k_th; i++)
{
for (int j = 0; j <= k - 2; j++)
printf("%lld ", dl[i].d[j]);
printf("%lld\n", dl[i].d[k - 1]);//最后一维的坐标后面不能加空格。
}
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
while (~scanf("%d%d", &n, &k)) //一直好奇前面的波浪号一样的是啥
{
input_data();
output_ans();
}
return 0;
}

【35.43%】【hdu 4347】The Closest M Points的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  4. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  5. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  6. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  7. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  8. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  9. 【规律】【贪心】【数学】HDU 5573 Binary Tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

随机推荐

  1. ASP.NET MVC 4 (十一) Bundles和显示模式--asp.net mvc中 @Scripts.Render("~/bundles/jquery")是什么意思? 在布局文件中使用Scripts.Render()输出脚本包,Styles.Render()输出风格包:

    ASP.NET MVC 4 (十一) Bundles和显示模式 ASP.NET MVC 4 引入的js打包压缩功能.打包压缩jquery目录下的文件,在布局文件中使用Scripts.Render()输 ...

  2. Spring学习总结(6)——Spring之核心容器bean

    一.Bean的基础知识 1.在xml配置文件中,bean的标识(id 和 name) id:指定在benafactory中管理该bean的唯一的标识.name可用来唯一标识bean 或给bean起别名 ...

  3. iOS 友盟分享

    iOS 友盟分享 这个主要是提到怎样通过友盟去自己定义分享的步骤: 一.肯定要去友盟官网下载最新的SDK包,然后将SDK导入到你的project目录里面去. 二.注冊友盟账号.将你的APP加入到你的账 ...

  4. NAACL 2013 Paper Mining User Relations from Online Discussions using Sentiment Analysis and PMF

    中文简单介绍:本文对怎样基于情感分析和概率矩阵分解从网络论坛讨论中挖掘用户关系进行了深入研究. 论文出处:NAACL'13. 英文摘要: Advances in sentiment analysis ...

  5. 8.Swift教程翻译系列——控制流之条件

    3.条件语句 常常会须要依据不同的情况来运行不同的代码. 你可能想要在错误发生的时候运行一段额外的代码,或者当某个值变得太高或者太低的时候给他输出出来.要实现这些需求,你能够使用条件分支. Swift ...

  6. uva 10098 Generating Fast(全排列)

    还是用的两种方法,递归和STL,递归那个是含有反复元素的全排列,这道题我 没有尝试没有反复元素的排列,由于从题目上并没有发现一定是有反复元素的() 贴代码: <span style=" ...

  7. WCF REST (一)

    最近工作中学习使用了WCF REST,REST 有很多好处 高效 简约  面向资源  而客户端调用 也变得非常简单.REST 入门的资料等 大家可以去网上找 这里主要分享下遇到的问题以及解决~ 一.环 ...

  8. 观察者模式 VS 责任链模式

    为什么要把观察者模式和责任链模式放在一起对比呢?这两个模式没有太多的相似性呀,真没有嘛?有相似性,我们在观察者模式中也提到了触发链(也叫做观察者链)的问题,一个具体的角色既可以是观察者,也可以是被观察 ...

  9. 【AtCoder Beginner Contest 074 A】Bichrome Cells

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...

  10. Access WMI via Python from Linux

    You can use Impacket (https://github.com/CoreSecurity/impacket) that has WMI implemented in Python. ...