说在前面
第一眼还以为是斯坦纳树…发现不对
去翻了题解,发现是kd-tree模板题???me可能是真的傻掉了
题目
题目大意
给出平面上N个点,现在需要从中选出一个点,使得 离该点最远的点到它的距离 与 离该点最近的点到它的距离 之差最小,输出最小的差值。
输入输出格式
输入格式:
第一行一个整数N,表示点数
接下来N行,每行一个二元组描述一个点的坐标
输出格式:
输出答案
解法
Kd-tree版子题,先构建好整棵树,然后对于每个点都询问最远点和最近点即可。
Kd-tree的复杂度,me在网上找了很久,最理性也只有一个公式:
Q(n)=2+2Q(n/4)⟹Q=N−−√
Q
(
n
)
=
2
+
2
Q
(
n
/
4
)
⟹
Q
=
N
,其中Q是一条线分割的区间数。
于是玄学复杂度可过= =
下面是自带大常数的代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
int N , ans , global_D , maxdis , mindis ;
template <typename T>
void smax( T&x , T y ){ if( x < y ) x = y ; }
template <typename T>
void smin( T&x , T y ){ if( x > y ) x = y ; }
template <typename T>
T abs_( T x ){ return x > 0 ? x : -x ; }
struct Node{
int d[2] , maxn[2] , minn[2] ;
Node *ch[2] ;
void update(){
maxn[0] = minn[0] = d[0] ;
maxn[1] = minn[1] = d[1] ;
if( ch[0] ){
smax( maxn[0] , ch[0]->maxn[0] ) ;
smax( maxn[1] , ch[0]->maxn[1] ) ;
smin( minn[0] , ch[0]->minn[0] ) ;
smin( minn[1] , ch[0]->minn[1] ) ;
} if( ch[1] ){
smax( maxn[0] , ch[1]->maxn[0] ) ;
smax( maxn[1] , ch[1]->maxn[1] ) ;
smin( minn[0] , ch[1]->minn[0] ) ;
smin( minn[1] , ch[1]->minn[1] ) ;
}
}
} w[500005] , *root ;
bool cmp( const Node &A , const Node &B ){
return A.d[ global_D ] < B.d[ global_D ] ||
( A.d[ global_D ] == B.d[ global_D ] && A.d[ global_D^1 ] < B.d[ global_D^1 ] ) ;
}
Node *build( int lf , int rg , int d ){
int mid = ( lf + rg ) >> 1 ;
global_D = d ;
nth_element( w + lf , w + mid , w + rg + 1 , cmp ) ;
Node *nd = &w[mid] ;
if( lf < mid ) nd->ch[0] = build( lf , mid - 1 , d^1 ) ;
if( rg > mid ) nd->ch[1] = build( mid + 1 , rg , d^1 ) ;
nd->update() ;
return nd ;
}
int dismax( Node *nd , int x , int y ){
int rt = 0 ;
rt += max( abs_( nd->maxn[0] - x ) , abs_( nd->minn[0] - x ) ) ;
rt += max( abs_( nd->maxn[1] - y ) , abs_( nd->minn[1] - y ) ) ;
return rt ;
}
int dismin( Node *nd , int x , int y ){
int rt = 0 ;
if( nd->minn[0] > x ) rt += nd->minn[0] - x ;
if( nd->maxn[0] < x ) rt += x - nd->maxn[0] ;
if( nd->minn[1] > y ) rt += nd->minn[1] - y ;
if( nd->maxn[1] < y ) rt += y - nd->maxn[1] ;
return rt ;
}
void Query_max( Node *nd , Node *aim ){
if( nd != aim )
smax( maxdis , abs_( nd->d[0] - aim->d[0] ) + abs_( nd->d[1] - aim->d[1] ) ) ;
int disL = 0 , disR = 0 ;
if( nd->ch[0] ) disL = dismax( nd->ch[0] , aim->d[0] , aim->d[1] ) ;
if( nd->ch[1] ) disR = dismax( nd->ch[1] , aim->d[0] , aim->d[1] ) ;
if( disL > disR ){
if( maxdis < disL ) Query_max( nd->ch[0] , aim ) ;
if( maxdis < disR ) Query_max( nd->ch[1] , aim ) ;
} else {
if( maxdis < disR ) Query_max( nd->ch[1] , aim ) ;
if( maxdis < disL ) Query_max( nd->ch[0] , aim ) ;
}
}
void Query_min( Node *nd , Node *aim ){
if( nd != aim )
smin( mindis , abs_( nd->d[0] - aim->d[0] ) + abs_( nd->d[1] - aim->d[1] ) ) ;
int disL = 0x3f3f3f3f , disR = 0x3f3f3f3f ;
if( nd->ch[0] ) disL = dismin( nd->ch[0] , aim->d[0] , aim->d[1] ) ;
if( nd->ch[1] ) disR = dismin( nd->ch[1] , aim->d[0] , aim->d[1] ) ;
if( disL < disR ){
if( mindis > disL ) Query_min( nd->ch[0] , aim ) ;
if( mindis > disR ) Query_min( nd->ch[1] , aim ) ;
} else {
if( mindis > disR ) Query_min( nd->ch[1] , aim ) ;
if( mindis > disL ) Query_min( nd->ch[0] , aim ) ;
}
}
int main(){
scanf( "%d" , &N ) ;
for( int i = 1 ; i <= N ; i ++ )
scanf( "%d%d" , &w[i].d[0] , &w[i].d[1] ) ;
root = build( 1 , N , 1 ) ;
ans = 0x3f3f3f3f ;
for( int i = 1 ; i <= N ; i ++ ){
maxdis = 0 ; Query_max( root , &w[i] ) ;
mindis = 0x3f3f3f3f ; Query_min( root , &w[i] ) ;
smin( ans , maxdis - mindis ) ;
}
printf( "%d" , ans ) ;
}