码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-19 13:43:06      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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 then技术分享 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 (x技术分享1技术分享,y技术分享1技术分享)技术分享 and (x技术分享2技术分享,y技术分享2技术分享)技术分享 as d=(x技术分享1技术分享?x技术分享2技术分享)技术分享2技术分享+(y技术分享1技术分享?y技术分享2技术分享)技术分享2技术分享技术分享.

Input
The first line of the input contains an integerT技术分享(1T5)技术分享, which denotes the number of testcases.
For each testcase, the first line contains an integers n技术分享(3n10技术分享5技术分享)技术分享, which denotes the number of temples in this testcase.
The following n技术分享 lines contains n技术分享 pairs of integers, the i技术分享-th pair (x,y)技术分享(?10技术分享5技术分享x,y10技术分享5技术分享)技术分享 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;
 }


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

标签:

原文地址:http://blog.csdn.net/liangzhaoyang1/article/details/51954274

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!