HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)
来源:互联网 发布:淘宝刷佣金是真是假 编辑:程序博客网 时间:2024/05/23 00:03
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 aren 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 distanced between palace (x1,y1) and (x2,y2) as dx1−x2)2+(y1−y2)2 .
There are
However, the underworld is mysterious and changes all the time. At different times, exactly one of the
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
Input
The first line of the input contains an integerT (1≤T≤5) , which denotes the number of testcases.
For each testcase, the first line contains an integersn (3≤n≤105) , which denotes the number of temples in this testcase.
The followingn lines contains n pairs of integers, the i -th pair (x,y) (−105≤x,y≤105) denotes the position of the i -th palace.
For each testcase, the first line contains an integers
The following
Output
For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.
Sample Input
130 01 12 2
Sample Output
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 $。
Source
BestCoder 2nd Anniversary
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
- HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)
- BestCoder 2nd Anniversary D hdu 5721 最近点对
- 【HDU5721 BestCoder 2nd AnniversaryD】【平面最近点对 分治写法+KD-tree写法】Palace 平面最近点对
- HDU 5721 Palace 平面最近点对
- BestCoder 2nd Anniversary Palace
- HDU 5721 Palace(平面最近点对(分治))
- 【HDU 5721】Palace(平面最近点对)
- BestCoder 2nd Anniversary-1004 Palace题解
- HDU 5719 BestCoder 2nd Anniversary Arrange (DP)
- HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)
- HDU 5718 oracle BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary-1005
- BestCoder 2nd Anniversary Oracle
- 实体类(VO,DO,DTO)的划分
- db2数据库的启动和关闭
- 无判断max
- 插入排序
- 二叉树非递归遍历
- HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)
- HDU-3065-病毒侵袭持续中-AC自动机
- 如何随时退出程序
- 一个简单的rpc框架实现(一)
- android的通过context对象读取私有文件
- HDU 1896 Stones
- 白菜之hashCode()方法重写及不同数据类型调用hashCode的方法
- 《linux学习》之NFS的测试
- 混淆