http://acm.hdu.edu.cn/showproblem.php?pid=1007
直接见代码吧。不过这个是N*logN*logN的
尽管如此,我怎么感觉我的比他们的还快???
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define mem0(a) memset(a,0,sizeof(a)) 17 #define mem1(a) memset(a,-1,sizeof(a)) 18 #define lson k<<1, L, mid 19 #define rson k<<1|1, mid+1, R 20 21 //typedef long long LL; 22 const double eps = 1e-12; 23 const int MAXN = 100005; 24 const int MAXM = 500005; 25 26 double min(double a, double b) { return a < b ? a : b; } 27 28 struct Point 29 { 30 double x; 31 double y; 32 }; 33 int numOfPoint; 34 Point points[MAXN], TempMerge[MAXN]; 35 Point ansPoint1, ansPoint2; 36 double closest; 37 38 void initPoints() 39 { 40 mem0(points); closest = INF; 41 for(int i=0;i<numOfPoint;i++) 42 { 43 scanf("%lf %lf", &points[i].x, &points[i].y); 44 } 45 } 46 47 int cmp_X(Point A, Point B) 48 { 49 if(A.x != B.x) return A.x < B.x; 50 return A.y < B.y; 51 } 52 53 int cmp_Y(Point A, Point B) 54 { 55 if(A.y != B.y) return A.y < B.y; 56 return A.x < B.x; 57 } 58 #define distance(A, B) sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)) 59 //double distance(Point &A, Point &B) 60 //{ 61 // return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); 62 //} 63 64 double calculateTheClosest(int low, int high) 65 { 66 for(int i=low;i<=high;i++)TempMerge[i-low] = points[i]; 67 sort(TempMerge, TempMerge+(high-low+1), cmp_Y); 68 for(int i=0;i<high-low;i++) 69 { 70 for(int j=i+1;j<=i+6 && j<=high-low;j++) 71 { 72 double calc = distance(TempMerge[i], TempMerge[j]); 73 if(calc < closest) 74 { 75 closest = calc; 76 ansPoint1 = TempMerge[i]; 77 ansPoint2 = TempMerge[j]; 78 } 79 } 80 } 81 return closest; 82 } 83 84 double findTheClosest(int left, int right) 85 { 86 if(left>=right) return INF; 87 int mid = (left+right)>>1; 88 double leftAns = findTheClosest(left, mid); 89 double rightAns = findTheClosest(mid+1, right); 90 double ans = min(leftAns, rightAns); 91 int low = left, high = mid+1; 92 while(distance(points[low], points[mid])>ans)low++; 93 while(high <= right && distance(points[high], points[mid])<=ans)high++; 94 ans = min(ans, calculateTheClosest(low, high-1)); 95 return ans; 96 } 97 98 int main() 99 { 100 while(scanf("%d", &numOfPoint) == 1 && numOfPoint) 101 { 102 initPoints(); 103 sort(points, points+numOfPoint, cmp_X); 104 double ans = findTheClosest(0, numOfPoint-1); 105 printf("%.2lf ", ans/2); 106 //printf("The Point(%.2lf, %.2lf) and Point(%.2lf, %.2lf) is %lf ", ansPoint1.x,ansPoint1.y,ansPoint2.x,ansPoint2.y,ans); 107 } 108 return 0; 109 }