HDU - 5721 Palace

The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld. 

There are nn palaces in the underworld, which can be located on a 2-Dimension plane with (x,y)(x,y) coordinates (where x,yx,y are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace. 

However, the underworld is mysterious and changes all the time. At different times, exactly one of the nn palaces disappears. 

Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared. 

Print the sum of the distance after every single palace has disappeared. 

To avoid floating point error, define the distance dd between palace (x1,y1)(x1,y1) and (x2,y2)(x2,y2) as d=(x1−x2)2+(y1−y2)2d=(x1−x2)2+(y1−y2)2.

 

Input

The first line of the input contains an integer TT (1≤T≤5)(1≤T≤5), which denotes the number of testcases. 

For each testcase, the first line contains an integers nn (3≤n≤105)(3≤n≤105), which denotes the number of temples in this testcase. 

The following nn lines contains nn pairs of integers, the ii-th pair (x,y)(x,y) (−105≤x,y≤105)(−105≤x,y≤105) denotes the position of the ii-th palace.

 

Output

For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.

 

Sample Input

1
3
0 0
1 1
2 2

 

Sample Output

12

 

Hint

If 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个点,每次删除一个点后求最小点对的距离的平方和dist,最后输出所有的dist总和

解题思路:平面分治最小点对的模板题,n个中距离最小的两个点会被计算n-2次,因为删除除这两点之外的其他点,最小点距还是这两个点,所以只需在计算当删除的是这两个点中的其中一个点时剩余点的最小点距

平面分治最小点对:https://mp.csdn.net/postedit/85268256

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e6;
const ll INF=1e18;
int X,Y;//最小两个点的下标 
ll dis; //当前最小距离 
struct node{
	ll x,y;
	int id;
}a[maxn],term[maxn];
bool cmpx(node a,node b)
{
	return a.x<b.x;
}
bool cmpy(node a,node b)
{
	return a.y<b.y;
}
ll dist(node a,node b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
void minpairs(int left,int right,int opt)//opt表示删除点的序号 
{
	if(left==right)
		return ;
	if(left+1==right)
	{
		if(a[left].id!=opt&&a[right].id!=opt)
		{
			if(dist(a[left],a[right])<dis)
			{
				dis=dist(a[left],a[right]);
				if(opt==-1)
				{
					X=a[left].id;
					Y=a[right].id; 
				}
			}
		}
		return ;
	}
	int mid=(left+right)/2;
	minpairs(left,mid,opt);
	minpairs(mid+1,right,opt);
	int i,j,k=0;
	for(i=left;i<=right;i++)
	{
		if(a[i].id!=opt&&abs(a[i].x-a[mid].x)<=dis)
			term[k++]=a[i];
	}
	sort(term,term+k,cmpy);
	for(i=0;i<k;i++)
	{
		for(j=i+1;j<k;j++)
		{
			if(term[j].y-term[i].y>dis)
				break;
			if(dist(term[j],term[i])<dis)
			{
				dis=dist(term[j],term[i]);
				if(opt==-1)
				{
					X=term[i].id;
					Y=term[j].id;
				}
			}
		}
	}
	return ;
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		ll ans=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%lld%lld",&a[i].x,&a[i].y);
			a[i].id=i;
		}
		dis=INF;
		sort(a,a+n,cmpx);
		minpairs(0,n-1,-1);
		ans+=dis*(n-2);
		dis=INF;
		minpairs(0,n-1,X);
		ans+=dis;
		dis=INF;
		minpairs(0,n-1,Y);
		ans+=dis;
		printf("%lld\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值