http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (Topic link)
Test instructions
Give the points on the N two-dimensional plane, and find the distance to the farthest point-the nearest point is the smallest.
Solution
Kdtree board, long to hear Jump said Kdtree are board questions →_→
An enumeration point that asks for its farthest point distance and nearest point distance to update the answer. Farthest nearest neighbor search is similar to the recent one, which is to change the valuation function.
Details
Code farming questions attention to detail
Code
bzoj1941#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #define LL long long#define inf 1<<30#define Pi acos ( -1.0) #define Free (a) Freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int Maxn=1000010,maxm=2;int n,k, ans1,ans2,rt;struct kdtree {int v[maxm],mn[maxm],mx[maxm],l,r;friend bool operator < (const kdtree A,const kdtree b) {R Eturn a.v[k]<b.v[k];}} Tr[maxn],s;int dis (kdtree a,kdtree b) {int res=0;for (int i=0;i<=1;i++) res+=abs (A.v[i]-b.v[i]); return res;} int eva1 (int k) {if (!k) return inf;int res=0;for (int i=0;i<=1;i++) Res+=max (0,s.v[i]-tr[k].mx[i]) +max (0,tr[k].mn[i] -s.v[i]); return res;} int eva2 (int k) {if (!k) return 0;int res=0;for (int i=0;i<=1;i++) Res+=max (Tr[k].mx[i]-s.v[i],s.v[i]-tr[k].mn[i]); return res;} void update (int k) {if (TR[K].L) for (int i=0;i<=1;i++) {Tr[k].mx[i]=max (tr[k].mx[i],tr[tr[k].l].mx[i]); Tr[k].mn[i] =min (tr[k].mn[I],tr[tr[k].l].mn[i]);} if (TR[K].R) for (int i=0;i<=1;i++) {Tr[k].mx[i]=max (tr[k].mx[i],tr[tr[k].r].mx[i]); Tr[k].mn[i]=min (Tr[k].mn[i], Tr[tr[k].r].mn[i]);}} int build (int l,int r,int p) {k=p;int mid= (l+r) >>1;nth_element (tr+l,tr+mid,tr+r+1); for (int i=0;i<=1;i++) tr[ Mid].mn[i]=tr[mid].mx[i]=tr[mid].v[i];if (L<mid) tr[mid].l=build (l,mid-1,p^1), if (Mid<r) Tr[mid].r=build (mid +1,R,P^1); update (mid); return mid;} void Query1 (int k) {if (S.v[0]!=tr[k].v[0] | | S.V[1]!=TR[K].V[1]) ans1=min (Ans1,dis (s,tr[k)), int dl=eva1 (TR[K].L), DR=EVA1 (TR[K].R), if (DL<DR) {if (dl<ans1 ) Query1 (TR[K].L); if (dr<ans1) Query1 (TR[K].R);} else {if (dr<ans1) Query1 (TR[K].R), if (dl<ans1) Query1 (TR[K].L);}} void Query2 (int k) {Ans2=max (Ans2,dis (S,tr[k])), int dl=eva2 (TR[K].L), Dr=eva2 (TR[K].R), if (DL>DR) {if (DL>ANS2) Query2 (TR[K].L); if (dr>ans2) Query2 (TR[K].R);} else {if (dr>ans2) Query2 (TR[K].R), if (DL>ANS2) Query2 (TR[K].L);}} int main () {scanf ("%d", &n); for (int i=1;i<=n;i+ +) scanf ("%d%d", &tr[i].v[0],&tr[i].v[1]); Rt=build (1,n,0); int ans=inf;for (int i=1;i<=n;i++) {s=tr[i]; Ans1=inf;query1 (RT); Minimum Ans2=0;query2 (RT); Max Ans=min (ANS,ANS2-ANS1);} printf ("%d", ans); return 0;}
"bzoj1941" Sdoi2010-hide and Seek