平面最近点对

本文介绍了一种解决一维和二维空间中寻找最接近点对问题的算法。在一维情况下,通过递归地将问题分解并求解子问题来找到最近的两点;在二维情况下,则结合了分治法和排序思想来高效地解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一维:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
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]<ans) ans=s[mid+1]-s[mid];
    return ans;
}
double Random()
{
    double result=rand()%10000;
     return result*0.01;
}
int main(){
    int n,m;
    //随机生成种子值
    srand(1);
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            s[i]=rand()%10000;
            printf("%lf ",s[i]);
        }
        printf("\n");
        sort(s,s+n);
        for(int i=0;i<n;i++)
            printf("%lf ",s[i]);
        printf("\n");
        printf("%.4lf\n",closest(0,n-1));
    }
}
二维:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#define mem0(a) memset(a,0,sizeof(a))
#define INF 0x3f3f3f3f
#define N 100005
using namespace std;
typedef long long LL;
const int MAXN = 100005;
//double min(double a, double b) { return a < b ? a : b; }
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<high-low;i++)
    {
        for(int j=i+1;j<=i+6 && j<=high-low;j++)
        {
            LL calc = distance(TempMerge[i], TempMerge[j]);
            if(calc < closest)
            {
                closest = calc;
                ansPoint1 = TempMerge[i];
                ansPoint2 = TempMerge[j];
            }
        }
    }
    return closest;
}
LL findTheClosest(int left, int right)
{
    if(left>=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<n;i++)
        scanf("%lld",&data[i]);
    sum[0]=data[0];
    for(LL i=1;i<n;i++)
        sum[i]=sum[i-1]+data[i];
    mem0(points); closest = INF;
    for(LL i=0;i<n;i++)
    {
        points[i].x=i;
        points[i].y=sum[i];
    }
    sort(points,points+n,cmp_X);
    LL ans=findTheClosest(0,n-1);
    printf("%lld\n",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值