如果你TLE122而且坐标开的int
查看原帖
如果你TLE122而且坐标开的int
600654
FishAndCat楼主2022/12/6 00:04

注意条件式里面也要乘上1ll!没注意到这点会导致溢出,加进很多不要的点

#include <bits/stdc++.h>
using namespace std;

struct node
{
	int x;
	int y;
}P[400010];
int tmp[400010];
long long dis(int i,int j)
{
	return 1ll*(P[i].x-P[j].x)*(P[i].x-P[j].x)+1ll*(P[i].y-P[j].y)*(P[i].y-P[j].y);
}
inline bool cmp(const node& a,const node& b)
{
	return a.x<b.x;
}
inline bool cmp1(const int& a,const int& b)
{
	return P[a].y<P[b].y;
}
long long shortest_dis(int l,int r)
{
	 long long d=1e16;
	 if(l==r)return d;
	 if(r-l==1)return dis(l,r);
	 int mid=(l+r)/2;
	 d=min(shortest_dis(l,mid),shortest_dis(mid+1,r));
	 int cnt=0;
     for(int i=l;i<=r;i++)
     if(1ll*(P[i].x-P[mid].x)*(P[i].x-P[mid].x)<d)//为什么只对x限制? 而且如果这样安排点集不是O(n^2)+O(nlogn)复杂度递推?
     tmp[++cnt]=i;               //y在下个循环中限制
     sort(tmp+1,tmp+1+cnt,cmp1);
     for(int i=1;i<=cnt;i++)
     	for(int j=i+1;j<=cnt&&1ll*(P[tmp[i]].y-P[tmp[j]].y)*(P[tmp[i]].y-P[tmp[j]].y)<d;j++)
     	{
     		d=min(d,dis(tmp[i],tmp[j]));
     	}
     return d;
  
}

int main()
{
 int n;
 ios::sync_with_stdio(false);
 cin.tie(0);
 cin>>n;
 for(int i=1;i<=n;i++)
 {
    cin>>P[i].x>>P[i].y;
 }
 sort(P+1,P+1+n,cmp);
 long long ans=shortest_dis(1,n);
 cout<<ans;
}
2022/12/6 00:04
加载中...