标签:
1 3 0 0 1 1 2 2
12HintIf palace $ (0,0) $ disappears,$ d = (1-2) ^ 2 + (1 - 2) ^ 2 = 2 $; If palace $ (1,1) $ disappears,$ d = (0-2) ^ 2 + (0 - 2) ^ 2 = 8 $; If palace $ (2,2) $ disappears,$ d = (0-1) ^ 2 + (0-1) ^ 2 = 2 $; Thus the answer is $ 2 + 8 + 2 = 12 $。
给出平面上n个点的坐标,可以求平面最近点对的距离。
要求分别删掉第1~n个点时,平面上最近点对的距离的和。(防止精度问题,题目要求是距离的平方)
题解:求平面最近点对。分治法。
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
typedef long long LL;
using namespace std;
const LL Max =1LL<<60;
//平面最近点对:分治
struct point
{
LL x,y;
};
point p[100010]; //数组开小会TLE
int t[1000010]; //数组开小会TLE
LL min(LL a,LL b)
{
return a>b?b:a;
}
int cmp (const point &a,const point &b) //对点的x坐标排序
{
if(a.x==b.x)return a.y<b.y;
return a.x>b.x;
}
int cmpy(const int &a,const int &b) //对点的y坐标排序
{
return p[a].y<p[b].y;
}
LL dis(point a,point b) //两点的距离
{
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
LL myabs(LL x)
{
return x<0?-x:x;
}
struct node
{
LL res;
int x,y;
node (){}
node (LL res,int x,int y):res(res),x(x),y(y){}
};
LL sqr(LL x)
{
return x*x;
}
node solve(int left,int right)//分治,递归
{
LL x=Max;
if(left==right)return node(x,0,0);
if(left==right+1)return node(dis(p[left],p[right]),left,right);
int mid=(left+right)>>1; //求出中间点
node dl=solve(left,mid);
node dr=solve(mid+1,right);
if(dl.res>dr.res)
dl=dr;
//把中间2dl宽度的部分的点作单独处理
int i,j,k;
k=0;
for(i=left;i<=right;i++)
{
if(sqr(p[i].x-p[i+1].x)<dl.res)
t[k++]=i;
}
sort(t,t+k,cmpy); //由下到上排序,扫描
for(i=0;i<k;i++)
{
for(j=i+1;j<k&&sqr(p[t[j]].y-p[t[i]].y)<dl.res;j++)
{
LL d3 =dis(p[t[i]],p[t[j]]);
if(dl.res>d3)
{
dl =node(d3,t[i],t[j]);
}
}
}
return dl;
}
int main()
{
int T;
cin>>T;
int n;
while(T--)
{
cin>>n;
int x,y;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
p[i].x =x; p[i].y =y;
}
sort(p+1,p+n+1,cmp);
node r=solve(1,n);
LL res =r.res*(n-2);
point tmp =p[r.x];
p[r.x].x=1e9;
p[r.x].y=1e9;
res+=solve(1,n).res;
p[r.x]=tmp;
p[r.y].x=1e9;
res+=solve(1,n).res;
cout<<res<<endl;
}
return 0;
}HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)
标签:
原文地址:http://blog.csdn.net/liangzhaoyang1/article/details/51954274