求助!
查看原帖
求助!
112631
Lovable_Wind楼主2022/10/27 23:03

TLE 110分

#include<bits/stdc++.h>
#define int long long
using namespace std;
const double pi=3.14;
const int inf=0x3f3f3f3f;
const int NIL=-1;
const int MOD=1e9+7;
const int MAXN=5e5+7;
#define pf(x) x*x
int n,m;
struct Node{
	int x,y;
}p[MAXN],q[MAXN];
int dis(const Node &a,const Node &b){
	 return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(const Node &a,const Node &b){
	return a.x<b.x;
}
int cdq(int l,int r){
	if (l==r) return 800000000114514;
	int mid=(l+r)>>1;
	int midx=p[mid].x;
	int d=min(cdq(l,mid),cdq(mid+1,r));
	int p1=l,p2=mid+1,tot=0;
	while(p1<=mid||p2<=r){
		if (p1<=mid&&(p2>r||p[p1].y<p[p2].y)){
			q[++tot]=p[p1++];
		}else{
			q[++tot]=p[p2++];
		}
	}
	for (int i=1;i<=tot;i++){
		p[i+l-1]=q[i];
	}
	tot=0;
	for (int i=l;i<=r;i++){
		if (pf((p[i].x-midx))<=d){
			q[++tot]=p[i];
		}
	}
	for (int i=1;i<=tot;i++){
		for (int j=i-1;j>=1&&pf(q[i].y-q[j].y)<=d;j--){
            d=min(d,dis(q[i],q[j]));
        }
        for (int j=i+1;j<=tot&&pf(q[j].y-q[i].y)<=d;j++){
            d=min(d,dis(q[i],q[j]));
        }
	}
	return d;
}
int read()
{
    int ans=0,flag=1;
    char ch=getchar();
    while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
    if(ch=='-') flag=-1,ch=getchar();
    while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
    return ans*flag;
}
signed main()
{
    ios :: sync_with_stdio (false);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=read();
	for (int i=1;i<=n;i++){
		p[i].x=read(),p[i].y=read();
	}
	sort(p+1,p+n+1,cmp);
	cout<<cdq(1ll,n)<<endl;
}

是我写假了吗?还是哪里不对呢

2022/10/27 23:03
加载中...