shoot_down @ 2023-07-27 21:40:19
RT,P1429 平面最近点对(加强版)的代码直接交了,
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
double x,y;
}a[400001];
bool cmp(node a,node b){
if(a.x==b.x) return a.y<b.y;
else return a.x<b.x;
}
bool cmp2(int a2,int b){
return a[a2].y<a[b].y;
}
double xy(int x,int y){
double xx=(a[y].x-a[x].x)*(a[y].x-a[x].x);
double yy=(a[y].y-a[x].y)*(a[y].y-a[x].y);
return sqrt(xx+yy);
}
double merg(int l,int r){
vector<int> v;
double ans=0x7ffffff;
if(l==r) return ans;
else if(l+1==r) return xy(l,r);
int mid=l+r>>1;
double minm=min(merg(l,mid),merg(mid+1,r));
for(int i=l;i<=r;i++) if(fabs(a[mid].x-a[i].x)<minm) v.push_back(i);
sort(v.begin(),v.end(),cmp2);
int sizev=v.size();
for(int i=0;i<sizev;i++){
for(int j=i+1;j<sizev&&fabs(a[v[i]].y-a[v[j]].y)<minm;j++){
minm=min(minm,xy(v[i],v[j]));
}
}
return minm;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y;
sort(a+1,a+1+n,cmp);
double ans=merg(1,n);
printf("%.0f",ans*ans);
return 0;
}
by 035966_L3 @ 2023-07-27 21:43:54
@shoot_down 数组开到 4.4e6,ans 开到 0x7ffffffffff,直接 AC……
by 035966_L3 @ 2023-07-27 21:44:03
https://www.luogu.com.cn/record/117754355
by 035966_L3 @ 2023-07-27 21:46:05
344/350 ms……orz!
by shoot_down @ 2023-07-27 22:01:20
@wosizmcy 数组和ans初值的问题问什么会MLE呢
by 035966_L3 @ 2023-07-27 22:08:12
@shoot_down 不知道……ans 不改也能 AC。
by 035966_L3 @ 2023-07-27 22:11:08
@shoot_down 似乎是递归爆栈……