hdu-5721 Palace(最近点对)
题目链接:
Palace
Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
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=(x1−x2)2+(y1−y2)2.
For each testcase, the first line contains an integers n (3≤n≤105), which denotes the number of temples in this testcase.
The following n lines contains n pairs of integers, the i-th pair (x,y) (−105≤x,y≤105) denotes the position of the i-th palace.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=998244353;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-6; struct node
{
LL x,y;
}po[N],temp[N],po1[N];
int cmp1(node a,node b)
{
return a.x<b.x;
}
int cmp2(node a,node b)
{
return a.y<b.y;
}
node fa,fb;
LL ans;
LL get_dis(node a, node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
LL merge(int l,int r)
{
if(r-l<=1)
{
if(l==r)return inf;
else
{
LL dis=get_dis(po[l],po[r]);
if(dis<ans)
{
fa=po[l];
fb=po[r];
ans=dis;
}
return dis;
}
} int mid=(l+r)>>1;
LL dl=merge(l,mid),dr=merge(mid+1,r),d=min(dl,dr);
int cnt=0;
For(i,l,r)
{
if(abs(po[mid].x-po[i].x)<=d)temp[++cnt]=po[i];
}
sort(temp+1,temp+cnt+1,cmp2);
For(i,1,cnt)
{
for(int j=i+1;j<=cnt&&temp[j].y-temp[i].y<d;j++)
{
LL dis=get_dis(temp[j],temp[i]);
if(dis<d)d=dis;
if(dis<ans)
{
ans=dis;
fa=temp[j];
fb=temp[i];
}
}
}
return d;
} LL work(int l,int r)
{
ans=inf;
sort(po+l,po+r+1,cmp1);
return merge(l,r);
}
int main()
{
int t;
read(t);
while(t--)
{
int n;
read(n);
For(i,1,n)read(po1[i].x),read(po1[i].y),po[i]=po1[i]; LL sum=0;
sum+=work(1,n)*(LL)(n-2);
node faa=fa,fbb=fb;
int flag=0;
For(i,1,n)
{
if(po1[i].x==faa.x&&po1[i].y==faa.y&&flag==0)
{
flag=1;
po[i].x=1e7;
po[i].y=1e7;
continue;
}
po[i]=po1[i];
}
sum+=work(1,n);
flag=0;
For(i,1,n)
{
if(po1[i].x==fbb.x&&po1[i].y==fbb.y&&flag==0)
{
flag=1;
po[i].x=1e7;
po[i].y=1e7;
continue;
}
po[i]=po1[i];
}
sum+=work(1,n);
cout<<sum<<"\n";
} return 0;
}
hdu-5721 Palace(最近点对)的更多相关文章
- HDU 1007Quoit Design(最近点问题)
最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点 思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的. 将区间一半一半的分开,直到分成只有一个点或两个点的时候! 对于只有两 ...
- hdu 4631(最近点对,容器)
点击打开链接 题意: 给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和. 给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c 如果按照平 ...
- Hdu 1007 最近点对
题目链接 Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)
题目链接:pid=5128">http://acm.hdu.edu.cn/showproblem.php? pid=5128 题面: The E-pang Palace Time Li ...
- HDU 4631 Sad Love Story 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意: 在平面内依次加点,求每次加点后最近点对距离平方的和 因为是找平面最近点对...所以加点以后这个最短 ...
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDU 5128 The E-pang Palace(2014广州赛区现场赛B题 计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 解题报告:在一个平面上给出n个点的坐标,用这n个点作为矩形的四个顶点,作两个矩形,要求两个矩形不 ...
- hdu 5128 The E-pang Palace
http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给定N个点,选出其中8个点组成两个矩形,使得两个矩形的面积和最大. 思路:找出所有的矩形,然后枚举, ...
随机推荐
- SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp
题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...
- python--错误了就需要调试(异常处理)
python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 我们可打开idle-->F1进行查看文档,里面很多异常类型,如图: ...
- T3137 栈练习1 codevs
codevs.cn/problem/3137 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈.先给出这些操作,请输出最终栈的栈顶元 ...
- Map类的三种实现 <个人练习>
package cn.zmh.zuoye; import java.util.HashMap; import java.util.Iterator; import java.util.Map; imp ...
- 服务器Out of Memory
之前有次把图片存储在数据库,结果读取时候报错了:Out of Memory.. 图片本来不应该存储在数据库中的,消耗内存太大,既然已经这样,那就先解决问题,不改存储方式. 如果一个应用程序为了提高性能 ...
- [转]gzip,bzip2,tar,zip命令使用方法详解
原文:http://blog.chinaunix.net/uid-20779720-id-2547669.html 1 gzipgzip(1) 是GNU的压缩程序.它只对单个文件进行压缩.基本用法如下 ...
- [转]Linux统计代码行数
wc -l *.c *.h 就可以知道当前目录下的所有c 和 h 文件的行数的详细信息.很不错 如果要递归,可以配合其他命令一起使用 当前目录及子目录: find . -name *.c |xargs ...
- Android eclipse 项目R文件无法生成
这个问题相信非常多人从网上已经搜到不少的解决方式. android 无法生成R文件 是件痛苦的事情.即使有时候你xml文件没有错误,他都无法生成. 针对此问题,我总结下面几个方面的原因. 一.xml本 ...
- android studio——Could not find method externalNativeBuild()
gradle同步工程时出现错误 Error:(36, 0) Could not find method externalNativeBuild() for arguments [build_cazi7 ...
- HTML制作练习