In case of failure
Time Limit: 60000/30000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2757 Accepted Submission(s): 1155
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.
参考资料:
15min理解KD树:https://www.jianshu.com/p/ffe52db3e12b
KD树详解:https://blog.csdn.net/acdreamers/article/details/44664645
基本思路:
建树(KD树中的每个节点都代表着一个点):
- 只考虑x轴坐标,找到中间点P作为当前节点,所有在P左边的点全部在左子树,所有在P右边的点全部在右子树;
- 再按y坐标继续分点,同上,之后继续执行操作①直到该区域剩下不到1个点
查询:
- 对于当前询问的点x,假设当前到P节点,先判断x与P的距离lenP
- 如果x在P的左子树,则递归左子树,求出最小值lenL,如果lenL>lenP,说明虽然x在P的左子树,但是离x最近的点可能在P的右子树中,还需递归P的右子树
- 如果x在P的右子树,同上
nth_element(begin, begin+k, end, comp):
使得第k+1大的元素刚好位于第k+1个位置,且比这个元素大的都在它们后面,comp为排序规则
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define deep 2
#define mod 1000000007
typedef struct Point
{
LL x[3];
}Point;
Point p[100015], temp[100015];
int loc;
bool comp(Point a, Point b) { return a.x[loc]<b.x[loc]; }
void build(int l, int r, int now)
{
int m;
if(l>=r)
return;
m = (l+r)/2;
loc = now%deep;
nth_element(p+l, p+m, p+r+1, comp);
build(l, m-1, now+1);
build(m+1, r, now+1);
}
LL Dis(Point x, Point y)
{
LL ans;
ans = 0;
for(int i=0;i<deep;i++)
ans += (x.x[i]-y.x[i])*(x.x[i]-y.x[i]);
if(ans==0) //假设出现点重合,重合的点就算做一个点了
return (LL)1e18;
return ans;
}
LL Find(Point x, int l, int r, int now)
{
LL ans, bet;
int cur, m;
cur = now%deep;
if(l>=r)
{
if(l==r)
return Dis(x, p[r]);
return (LL)1e18;
}
m = (l+r)/2;
ans = Dis(x, p[m]);
if(x.x[cur]<p[m].x[cur])
{
bet = Find(x, l, m-1, now+1);
if(bet>(x.x[cur]-p[m].x[cur])*(x.x[cur]-p[m].x[cur]))
bet = min(bet, Find(x, m+1, r, now+1));
}
else
{
bet = Find(x, m+1, r, now+1);
if(bet>(x.x[cur]-p[m].x[cur])*(x.x[cur]-p[m].x[cur]))
bet = min(bet, Find(x, l, m-1, now+1));
}
return min(ans, bet);
}
int main(void)
{
int T, n, i, j;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=0;j<=deep-1;j++)
scanf("%lld", &temp[i].x[j]);
p[i] = temp[i];
}
build(1, n, 0);
for(i=1;i<=n;i++)
printf("%lld\n", Find(temp[i], 1, n, 0));
}
return 0;
}