k-d树(hdu2966)

In case of failure

Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1505    Accepted Submission(s): 589


Problem Description
To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the customer to calmly head to the nearest Machine (that should hopefully
work fine).


In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.
 

Input
The input contains several test cases. The very first line contains the number of cases t (t <= 15) that follow. Each test cases begin with the number of Cash Machines n (2 <= n <= 10^5). Each of the next n lines contain the coordinates of one Cash Machine x,y (0 <= x,y <=10^9) separated by a space. No two
points in one test case will coincide.
 

Output
For each test case output n lines. i-th of them should contain the squared distance between the i-th ATM from the input and its nearest neighbour.
 

Sample Input
  
  
2 10 17 41 0 34 24 19 8 28 14 12 45 5 27 31 41 11 42 45 36 27 15 0 0 1 2 2 3 3 2 4 0 8 4 7 4 6 3 6 1 8 0 11 0 12 2 13 1 14 2 15 0
 

Sample Output
  
  
200 100 149 100 149 52 97 52 360 97 5 2 2 2 5 1 1 2 4 5 5 2 2 2 5


题意:求离每个点最近的点的距离

思路:k-d树

k-d树可以参考下面的博客:从K近邻算法、距离度量谈到KD树、SIFT+BBF算法 


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;

const LL INF=1e18;
const int maxn=100010;

int N;
struct Point
{
    LL x[3];
}p[maxn],ori[maxn];

int split[20],cur,dim;

bool cmp(Point a,Point b)
{
    return a.x[cur]<b.x[cur];
}
template<class T> T sqr(T x){return x*x;};
void build(int l,int r,int depth)
{
    if(l>=r)return;
    int mid=(l+r)>>1;
    cur=depth%dim;
    nth_element(p+l,p+mid,p+r+1,cmp);
    build(l,mid-1,depth+1);
    build(mid+1,r,depth+1);
}

LL dis(Point x,Point y)
{
    LL ret=0;
    for(int i=0;i<dim;i++)
        ret+=sqr(x.x[i]-y.x[i]);
    return ret?ret:INF;
}
LL find(Point x,int l,int r,int depth)
{
    int cur=depth%dim;
    if(l>=r)
    {
        if(l==r)return dis(x,p[l]);
        return INF;
    }
    int mid=(l+r)>>1;
    LL ret=dis(x,p[mid]),tmp;
    if(x.x[cur]<p[mid].x[cur])
    {
        tmp=find(x,l,mid-1,depth+1);
        if(tmp>sqr(x.x[cur]-p[mid].x[cur]))
            tmp=min(tmp,find(x,mid+1,r,depth+1));
    }
    else
    {
        tmp=find(x,mid+1,r,depth+1);
        if(tmp>sqr(x.x[cur]-p[mid].x[cur]))
            tmp=min(tmp,find(x,l,mid-1,depth+1));
    }
    return min(ret,tmp);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        dim=2;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<dim;j++)
                scanf("%I64d",&ori[i].x[j]);
            p[i]=ori[i];
        }
        build(0,N-1,0);
        for(int i=0;i<N;i++)
            printf("%I64d\n",find(ori[i],0,N-1,0));
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值