HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)

23 篇文章 0 订阅
20 篇文章 5 订阅

Palace

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 465    Accepted Submission(s): 118

Problem Description
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 n palaces in the underworld, which can be located on a 2-Dimension plane with (x,y) coordinates (where x,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 n 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 d between palace (x1,y1) and (x2,y2) as d=(x1x2)2+(y1y2)2 .

Input
The first line of the input contains an integer T (1T5) , which denotes the number of testcases.
For each testcase, the first line contains an integers n (3n105) , which denotes the number of temples in this testcase.
The following n lines contains n pairs of integers, the i -th pair (x,y) (105x,y105) denotes the position of the i -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 $。
Source
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5717 5716 5715 5714 5713 
 
题意:

给出平面上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;
 }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值