分治法求最近点对

一维最近点对(数轴上)

模版:

#include

using namespace std;

const int inf=0x3f3f3f3f;//赋大值

double s[100];

double mn;

double closest(int low,int high)
{
    if(low+1==high) 
        return s[high]-s[low];
    if(low+2==high) 
    return min(s[low+1]-s[low],s[high]-s[low+1]);
    int mid=(low+high)>>1;
    double ans=min(closest(low,mid),closest(mid+1,high));
    if(s[mid+1]-s[mid]

二维(一个平面上)有n个点

求距离两个点之间的最近距离(最近点对)

模版:

#include
#define INF 0x3f3f3f3f//取大值
#define N 100005
using namespace std;

typedef long long LL;
const int MAXN = 100005;

struct Point
{
    LL x,y;
};

int numOfPoint;
Point points[MAXN], TempMerge[MAXN];
Point ansPoint1, ansPoint2;
LL closest;

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) (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)
LL calculateTheClosest(int low, int high)
{
    for(int i=low;i<=high;i++)TempMerge[i-low] = points[i];
    sort(TempMerge, TempMerge+(high-low+1), cmp_Y);
    for(int i=0;i=right)
        return INF;
    int mid = (left+right)>>1;
    LL leftAns = findTheClosest(left, mid);
    LL rightAns = findTheClosest(mid+1, right);
    LL ans = min(leftAns, rightAns);
    int low = left, high = mid+1;
    while(distance(points[low], points[mid])>ans)
        low++;
    while(high <= right && distance(points[high], points[mid])<=ans)
        high++;
    ans = min(ans, calculateTheClosest(low, high-1));
    return ans;
}
LL data[100005];
LL sum[100005];
int main()
{
    LL n;
    scanf("%lld",&n);
    for(LL i=0;i





你可能感兴趣的:(算法)