HDU1007最近点对(分治)

时间:2023-02-02 13:35:38

http://acm.hdu.edu.cn/showproblem.php?pid=1007

直接见代码吧。不过这个是N*logN*logN的

尽管如此,我怎么感觉我的比他们的还快???

HDU1007最近点对(分治)

HDU1007最近点对(分治)

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R //typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; double min(double a, double b) { return a < b ? a : b; } struct Point
{
double x;
double y;
};
int numOfPoint;
Point points[MAXN], TempMerge[MAXN];
Point ansPoint1, ansPoint2;
double closest; void initPoints()
{
mem0(points); closest = INF;
for(int i=;i<numOfPoint;i++)
{
scanf("%lf %lf", &points[i].x, &points[i].y);
}
} int cmp_X(Point A, Point B)
{
if(A.x != B.x) return A.x < B.x;
return A.y < B.y;
} int cmp_Y(Point A, Point B)
{
if(A.y != B.y) return A.y < B.y;
return A.x < B.x;
}
#define distance(A, B) sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y))
//double distance(Point &A, Point &B)
//{
// return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
//} double calculateTheClosest(int low, int high)
{
for(int i=low;i<=high;i++)TempMerge[i-low] = points[i];
sort(TempMerge, TempMerge+(high-low+), cmp_Y);
for(int i=;i<high-low;i++)
{
for(int j=i+;j<=i+ && j<=high-low;j++)
{
double calc = distance(TempMerge[i], TempMerge[j]);
if(calc < closest)
{
closest = calc;
ansPoint1 = TempMerge[i];
ansPoint2 = TempMerge[j];
}
}
}
return closest;
} double findTheClosest(int left, int right)
{
if(left>=right) return INF;
int mid = (left+right)>>;
double leftAns = findTheClosest(left, mid);
double rightAns = findTheClosest(mid+, right);
double ans = min(leftAns, rightAns);
int low = left, high = mid+;
while(distance(points[low], points[mid])>ans)low++;
while(high <= right && distance(points[high], points[mid])<=ans)high++;
ans = min(ans, calculateTheClosest(low, high-));
return ans;
} int main()
{
while(scanf("%d", &numOfPoint) == && numOfPoint)
{
initPoints();
sort(points, points+numOfPoint, cmp_X);
double ans = findTheClosest(, numOfPoint-);
printf("%.2lf\n", ans/);
//printf("The Point(%.2lf, %.2lf) and Point(%.2lf, %.2lf) is %lf\n", ansPoint1.x,ansPoint1.y,ansPoint2.x,ansPoint2.y,ans);
}
return ;
}