【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=1007
【算法】
答案为平面最近点对距离除以2
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 100010 const double INF = 1e10; struct info { double x,y; } a[MAXN]; int n,i; inline bool cmpx(info a,info b) { return a.x < b.x; } inline bool cmpy(info a,info b) { return a.y < b.y; } inline double dist(info p,info q) { return sqrt(abs(p.x - q.x) * abs(p.x - q.x) + abs(p.y - q.y) * abs(p.y - q.y)); } inline double Closest_Pair(int l,int r) { int i,j,mid,len = 0; static info s[MAXN]; double d; if (l == r) return INF; if (l + 1 == r) return dist(a[l],a[r]); mid = (l + r) >> 1; d = min(Closest_Pair(l,mid),Closest_Pair(mid+1,r)); for (i = l; i <= r; i++) { if (abs(a[mid].x - a[i].x) <= d) s[++len] = a[i]; } sort(s+1,s+len+1,cmpy); for (i = 1; i <= len; i++) { for (j = i + 1; j <= len && s[j].y - s[i].y <= d; j++) { d = min(d,dist(s[i],s[j])); } } return d; } int main() { while (scanf("%d",&n) && n) { for (i = 1; i <= n; i++) scanf("%lf%lf",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmpx); printf("%.2lf\n",Closest_Pair(1,n) / 2.0); } return 0; }
原文地址:https://www.cnblogs.com/evenbao/p/9240696.html
时间: 2024-10-06 19:09:29