HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007
简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少。。
//STATUS:G++_AC_1703MS_5004KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End
struct Node{ //id为nod排序后的编号值,index为排序前的标号值(随便自己定义)
double x,y;
int id,index;
Node(){}
Node(double _x,double _y,int _index):x(_x),y(_y),index(_index){}
}nod[N],temp[N]; int n; double dist(Node &a,Node &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmpxy(Node a,Node b) //先按x排序,然后按y排序
{
return a.x!=b.x?a.x<b.x:a.y<b.y;
} bool cmpy(Node a,Node b) //按y排序
{
return a.y<b.y;
} pii Closest_Pair(int l,int r) //返回排序后点的编号
{
if(l==r || l+==r)return pii(l,r); //只有一个点或者两个点
double d,d1,d2;
int i,j,k,mid=(l+r)/;
//左右两边最小距离点的编号
pii pn1=Closest_Pair(l,mid);
pii pn2=Closest_Pair(mid+,r);
//左右两遍的最小距离
d1=(pn1.first==pn1.second?OO:dist(nod[pn1.first],nod[pn1.second]));
d2=(pn2.first==pn2.second?OO:dist(nod[pn2.first],nod[pn2.second]));
pii ret;
d=Min(d1,d2);
ret=d1<d2?pn1:pn2;
//找出在mid-d,mid+d之间的点
for(i=l,k=;i<=r;i++){
if(fabs(nod[mid].x-nod[i].x)<=d){
temp[k++]=nod[i];
}
}
//合并两边,求最小距离
sort(temp,temp+k,cmpy);
for(i=;i<k;i++){
for(j=i+;j<k && fabs(temp[j].y-temp[i].y)<d;j++){
if(dist(temp[i],temp[j])<d){
d=dist(temp[i],temp[j]);
ret=make_pair(temp[i].id,temp[j].id);
}
}
} return ret;
} void Init() //初始化点
{
int i;
double x,y;
for(i=;i<n;i++){
scanf("%lf%lf",&x,&y);
nod[i]=Node(x,y,i);
}
sort(nod,nod+n,cmpxy);
for(i=;i<n;i++)nod[i].id=i; //排序后节点编号
} int main(){
// freopen("in.txt","r",stdin);
int T,i,j;
while(~scanf("%d",&n) && n)
{
Init();
pii ans=Closest_Pair(,n-); printf("%.2lf\n",dist(nod[ans.first],nod[ans.second])/);
}
return ;
}
HDU-1007 Quoit Design 平面最近点对的更多相关文章
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDU 1007 Quoit Design | 平面分治
暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- hdu 1007 Quoit Design(平面最近点对)
题意:求平面最近点对之间的距离 解:首先可以想到枚举的方法,枚举i,枚举j算点i和点j之间的距离,时间复杂度O(n2). 如果采用分治的思想,如果我们知道左半边点对答案d1,和右半边点的答案d2,如何 ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1007 Quoit Design 分治求最近点对
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1007 Quoit Design(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1007 Quoit Design
传送门 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- HDU 1007 Quoit Design(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- pdb文件 小结
.pdb文件,是VS生成的用于调试的符号文件(program database),保存着调试的信息.在VS的工程属性,C/C++,调试信息格式,设置/Zi,那么VS就会在构建项目时创建PDB文件. 在 ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- ASP.NET 4.5.256 has not been registered on the Web server
请见:http://answers.microsoft.com/en-us/insider/forum/insider_apps-insider_other/aspnet-45256-has-not- ...
- Cobalt Strike
http://www.77169.com/hack/201512/222080.shtm
- ArcGIS Engine 几何对象和WKB的转换
using System; using System.Collections.Generic; using System.Text; using GisSharpBlog.NetTopologySui ...
- C++11内存模型的粗略解释
基本解释 C++11引入了多线程,同时也引入了一套内存模型.从而提供了比较完善的一套多线程体系.在单线程时代,一切都很简单.没有共享数据,没有乱序执行,所有的指令的执行都是按照预定的时间线.但是也正是 ...
- Playing with cubes II
Description: Hey Codewarrior! You already implemented a Cube class, but now we need your help again! ...
- Android 内存管理分析(四)
尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...
- many-to-one和one-to-many的配置比较
many-to-one配置: <many-to-one name="dailyCatalog" column="daily_catalog_id" cla ...
- 【转】Android AlertDialog 点击对话框外部区域不关闭的设置
原文网址:http://blog.sina.com.cn/s/blog_8f1c79dd0101a63u.html 在Android开发中,常常需要调用对话框,但会遇到这样一种情况,在显示对话框的时候 ...