1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <bits/stdc++.h> #define int long long int #define ios ios::sync_with_stdio(0),cin.tie(0) #define N 200005 #define INF 1000000000LL #define swift 1000000000 using namespace std; int n,ans; double r,d; int dx[25] = {-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,0,0,0,0,0,1,1,1,1,1,2,2,2,2,2}; int dy[25] = {-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2}; unordered_map<int, int> m;
void solve(); inline void init(); void solve(); bool insert(int,int,int); inline double dis(int,int); inline int Grid(int);
struct node{ int x,y,ind; }point[N];
inline void init(){ m.clear(); }
inline int Grid(int ind){ int x = point[ind].x/r; int y = point[ind].y/r; return x*INF+y; } inline int dist(node a,node b){ int x = a.x-b.x,y = a.y-b.y; return (x*x+y*y); }
inline double dis(node a,node b){ int x = a.x-b.x,y = a.y-b.y; return sqrt(x*x+y*y); }
void solve(){ m.insert(make_pair(Grid(0),0));m.insert(make_pair(Grid(1),1)); for(int ind = 2;ind < n;ind++){ int x = point[ind].x/r,y = point[ind].y/r,better=0; for(int i=0;i<25;i++){ int nx = x+dx[i],ny = y+dy[i]; auto it = m.find(nx*INF+ny); if(it!=m.end()){ double distance = dis(point[it->second],point[ind]); if(distance<d){ better = 1; ans = dist(point[it->second],point[ind]); d = distance; r = d/2; } } } if(better){ m.clear(); for(int i=0;i<=ind;i++)m.insert(make_pair(Grid(i),i)); } else{ m.insert(make_pair(Grid(ind), ind)); } } }
signed main(){ ios; init(); cin>>n; for(int i=0;i<n;i++){ int x,y;cin>>x>>y; x+=swift;y+=swift; point[i].x = x;point[i].y = y; } random_shuffle(point, point+n); int smalln = sqrt(n); ans = dist(point[0],point[1]); d = dis(point[0], point[1]); for(int i=0;i<=smalln;i++){ for(int j=i+1;j<=smalln;j++){ d = min(d,dis(point[i], point[j])); ans = min(ans,dist(point[i],point[j])); } } r = d/2; solve(); cout<<ans<<endl; }
|