最近点对HDU1007
利用二分的方法来计算,应该是说利用分治的方法吧! 刚开始感觉时间会爆 后来发现嘎嘎居然没有 ,嗨自己算错了时间:
#include <iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
struct point
{
double x,y;
point (double a=0,double b=0)
{
x=a;y=b;
}
};
struct point p[100005];
int a[100005];
bool cmp1(point a,point b)
{
if(a.x<b.x)return true;
else return false;
}
bool cmp2(int a,int b)
{
if(p[a].y<p[b].y)return true;
else return false ;
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double min_value(double a,double b){ return a>b?b:a;}
double find(int l,int r)
{
if(l+1==r) return dist(p[l],p[r]);
if(l+2==r)
{
double a1=dist(p[l],p[l+1]);
double a2=dist(p[l+1],p[r]);
double a3=dist(p[l],p[r]);
a1= min_value(a1,min_value(a2,a3));
return a1;
}
int mid,i,num,j;
double ans;
mid=(l+r)/2;
num=0;
ans=min_value(find(l,mid),find(mid+1,r));
for(i=l;i<=r;i++)
if(p[i].x>=p[mid].x-ans&&p[i].x<=p[mid].x+ans)
a[num++]=i;
sort(a,a+num,cmp2);
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
{
if(p[a[j]].y-p[a[i]].y>ans)break;
ans=min_value(ans,dist(p[a[j]],p[a[i]]));
}
return ans; } int main()
{
double x,y;
int n,i;
while(scanf("%d",&n)==1)
{
if(n==0)break;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&x,&y);
p[i]=point(x,y);
}
sort(p,p+n,cmp1);
printf("%.2lf\n",find(0,n-1)/2);
} return 0;
}
最近点对HDU1007的更多相关文章
- Quoit Design(hdu1007)最近点对问题。模版哦!
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU1007(最近点对)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- (hdu1007)Quoit Design,求最近点对
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- hdu1007 平面最近点对(暴力+双线程优化)
突发奇想,用双线程似乎可以优化一些暴力 比如说平面最近点对这个题目,把点复制成2份 一份按照x排序,一份按照y排序 然后双线程暴力处理,一份处理x,一份处理y 如果数据利用x递减来卡,那么由于双线程, ...
- HDU1007最近点对(分治)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- 【hdu1007】最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 分治法的经典应用,复杂度可以证明为nlognlogn #include <iostream> ...
- ICP算法(Iterative Closest Point迭代最近点算法)
标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...
- Quoit Design---hdu1007(最近点对问题 分治法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:给你n(2<=n<=10^6)个点的坐标,然后找到两个点使得他们之间的距离最小 ...
随机推荐
- Apache SSL 服务搭建
Web服务器在默认情况下使用HTTP,这是一个纯文本的协议.正如其名称所暗示的,纯文本协议不会对传输中的数据进行任何形式的加密.而基于HTTP的Web服务器是非常容易配置,它在安全方面有重大缺陷.任何 ...
- MVVM软件设计模式(转)
add by zhj: MVVM是一种软件设计模式,这里要说一下设计模式,我们通常所的设计模式是指面向对象中的设计模式,用在面向对象编程语言中.但软件设计模式是更高一个级别的设计模式,两者不是同一个东 ...
- project proposal写作框架
主要有八个因素: 背景(Your Background):对于proposal有意义的要点,如国家职业证书.技能.经验.能力和实习经历等. 大纲(Outline Proposal):描述你明确的感兴趣 ...
- finecms如何批量替换文章中的关键词?
Finecms批量替换文章关键词要怎么操作呢,比如把关键词A换为B?Finecms是免费开源无商业限制的内容管理系统,个人在维护,但二次开发很灵活,我们可以通过开发插件或数据库sql语句来操作,下面就 ...
- 云serverlinux又一次挂载指定文件夹(非扩充)
版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/liuensong/article/details/27548771 新买的香港云server,系统仅仅能在 ...
- react-native-image-picker iOS设置
效果图,因为我用的是模拟器,所以拍照功能没有效果,不过可从相册选择,下面是具体的效果图 把react-native-image-picker添加到项目 yarn add react-native-im ...
- Python3学习之路~6.7 经典类和新式类的继承顺序
在Python中,经典类(class Person:)和新式类(class Person(object):)的主要区别就是体现在多继承的顺序上. Python 2.x中默认都是经典类,只有显式继承了o ...
- 执行hadoop自带的WordCount实例
hadoop 自带的WordCount实例可以统计一批文本文件中各单词出现的次数.下面介绍如何执行WordCount实例. 1.启动hadoop [root@hadoop ~]# start-all. ...
- 231A
#include <iostream> using namespace std; #define maxSize 10 int main() { int views[maxSize]; i ...
- git pull和push冲突
http://blog.csdn.net/matrix_laboratory/article/details/18034509 问题描述 1 有人改动了 文件A.java,提交到git 2 我没有pu ...