最近点对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)个点的坐标,然后找到两个点使得他们之间的距离最小 ...
随机推荐
- RHEL6.2 ORACLE11G
今天翻文件看到这个系统的安装截图才发现自己没写,补上. 惯例还是用到的所有参数均位于文末附录 启动虚拟机进入系统安装 选择跳过硬盘检测 选择语言 选择基本存储设备 设定主机名 设定口令 选择使用所有空 ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- inet_addr()和inet_ntoa()使用注意
inet_addr():无法处理255.255.255.255,认为该ip为非法,返回-1 inet_ntoa():转换后地址存储在静态变量中,连续两次调用,第二次会覆盖第一次的值. 建议使用inet ...
- 关于linux特殊含义的转义符\033
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- JavaScript, DOM查找元素
1.document.getElementById("id"); => IE8 及较低版本不区分ID的大小写 => IE7及较低版本中表单元素的name特性和ID都会被 ...
- 【Loadrunner】Loadrunner 手动关联技术
Loadrunner 手动关联技术 录制成功,回放失败,怀疑和动态数据有关: 1 重新录制一份脚本,两次录制的脚本进行比对,确定动态数据,复制动态数据: 2 找到第一次产生该动态数据的响应对应的相应 ...
- [phomeflashpic]怎样调用帝国CMS图片幻灯效果
今天改网站时试着用帝国cms的[phomeflashpic]标签调用图片作为首页幻灯片,默认是[phomeflashpic]0,3,300,235,0,0,13,3[/phomeflashpic],其 ...
- MySQL语法和用户授权
管理数据库 create database 等同于 create schema #导入数据库脚本 MariaDB [db1]> source /root/mysql/hellodb_in ...
- [django]django配合前端vue前后端联调,django服务端解决跨域(django-cors-headers)
django内部csrf post提交数据解决 https://www.cnblogs.com/iiiiiher/articles/9164940.html 前端写了个页面,里面$.post发现403 ...
- [sh]rm -rf*的防护和普通用户执行命令
尽量用普通用户执行,因为普通用户无法删除root的文件,避免误删除 rm -rf 不可取, 尽量find+rm -rf 尽量cd && rm -rf * 加上逻辑 cd /tmp/re ...