mle爆内存了是怎么回事0w0
查看原帖
mle爆内存了是怎么回事0w0
1196460
lawhello楼主2024/8/13 22:45

代码:

#include<bits/stdc++.h>
#include<algorithm>
#define ll long long
#define maxn 250000
#define INF 0x3f3f3f3f
using namespace std;
int n;
struct Point{
    double x;
    double y;
}pt[maxn],tmpt[maxn];
int cmp(Point a,Point b){
    return (a.x<b.x||(a.x==b.x&&a.y<b.y));
}
int cmpy(Point a,Point b)
{
    return (a.y<b.y);
}
double dist(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double closest_pair(int l,int r)
{
    if(l==r)
    return (double)INF;
    if(l+1==r)
    return dist(pt[l],pt[r]);
    int mid=(r-l)>>1;
    double d=min(closest_pair(l,mid),closest_pair(mid+1,r));
    int cnt=0;
    for(int i=l;i<=r;i++)
        if(fabs(pt[i].x-pt[mid].x)<d)
            tmpt[cnt++]=pt[i];
    sort(tmpt,tmpt+cnt,cmpy);//创建临时数组
    for(int i=0;i<cnt;i++)
        for(int j=i-1;j>=0&&(tmpt[i].y-tmpt[j].y)<d;j--)
        d=min(d,dist(tmpt[i],tmpt[j]));
    return d;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    scanf("%lf%lf",&pt[i].x,&pt[i].y);
    sort(pt,pt+n,cmp);
    printf("%.4f",closest_pair(0,n-1));
    system("pause");
}
2024/8/13 22:45
加载中...