zoukankan      html  css  js  c++  java
  • 【35.43%】【hdu 4347】The Closest M Points

    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:
    ", 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
    ", 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;
    }




  • 相关阅读:
    vue+ element table如何给指定的单元格添加点击事件
    nodejs 笔记
    sublime text3 编辑器如何运行js文件
    控制台运行nodejs程序
    控制台命令
    类似性别(0、1)判断的table列表数据渲染
    vue moment库格式化处理日期
    用百度siteapp的uaredirect.js判断用户访问端而进行域名的自动跳转
    编写email邮件的html页面注意事项
    IE6常见bug总结
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632250.html
Copyright © 2011-2022 走看看