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
| void Nearestpoint(point *a,int l,int r,int &u,int &v,double &d){
if(l==r) return ; if(l+1==r) { if(disPointPoint(a[l],a[r])<d) d=disPointPoint(a[l],a[r]),u=l,v=r; return ; } int mid=l+r>>1,m=0; Nearestpoint(a,l,mid,u,v,d),Nearestpoint(a,mid+1,r,u,v,d); point b[r-l+10]; for(int i=l;i<=r;i++){ if(abs(a[i].x-a[mid].x)<d) b[++m]=a[i]; } sort(b+1,b+1+m,sortYupXup); for(int i=1;i<=m;i++){ for(int j=i+1;j<=m&&abs(b[i].y-b[j].y)<d;j++){ if(d>disPointPoint(b[i],b[j]))d=disPointPoint(b[i],b[j]),u=i,v=j; } } return ; }
double mindisset1(point* a,int size,int &u,int &v){ double minn=1e18; sort(a+1,a+1+size,sortXupYup); Nearestpoint(a,1,size,u,v,minn); return minn; }
double mindisset2(point* u,int size){ double ans=1e20; struct cmp_y { bool operator()(const point &a, const point &b) const { return a.y < b.y; } }; multiset<point,cmp_y>s; for (int i = 1, l = 1; i <=size; i++) { while (l < i && u[i].x - u[l].x >= ans) s.erase(s.find(u[l++])); for (auto it = s.lower_bound(point{u[i].x, u[i].y - ans}); it != s.end() && it->y - u[i].y < ans; it++){ if(disPointPoint(*it,u[i])<ans) ans=disPointPoint(*it,u[i]); } s.insert(u[i]); } return ans; }
|