uva 10245 近期点对问题
分治法的典例
当练手了
奇妙的是。使用inplace_merge按说应该是O(n)的算法。可是用sort nlogn的算法反而更快
先上快排版
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int SIZE = 10000+10;
const double INF = 100000; struct Point
{
double x,y;
}p[SIZE],q[SIZE]; int n; inline double dis(const Point a, const Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmpx(const Point &a, const Point &b)
{
return a.x<b.x;
} bool cmpy(const Point &a, const Point &b)
{
return a.y<b.y;
} double MinDis(int l,int r)
{
if(r-l<1)return INF;
if(r-l==1)return dis(p[r],p[l]); int m=(l+r)/2;
double d=min(MinDis(l,m),MinDis(m+1,r));
int left=l,right=r+1;
int i;
for(i=m;i>=l;i--)
if(p[i].x<p[m].x-d)
{
left=i+1;
break;
}
for(i=m;i<=r;i++)
if(p[i].x>p[m].x+d)
{
right=i;
break;
}
for(int i=left;i<right;i++)q[i]=p[i];
sort(q+left,q+right,cmpy);
int j;
double ret=d;
for(int i=left;i<right;i++)
{
for(j=i+1;j<right && q[j].y-q[i].y<d;j++)
{
ret=min(ret,dis(q[i],q[j]));
}
}
return ret; } int main()
{
double ans; while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
ans=MinDis(0,n-1);
if(ans>10000)printf("INFINITY\n");
else printf("%.4lf\n",ans);
} return 0;
}
再上归并
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int SIZE = 10000+10;
const double INF = 100000;
#define dis(a,b) sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))
#define Fabs(x) x>0?x:-x
const double eps=1e-9; struct Point
{
double x,y;
}p[SIZE],q[SIZE]; int n; /*inline double dis(const Point a, const Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}*/ bool cmpx(const Point &a, const Point &b)
{
return a.x<b.x;
} bool cmpy(const Point &a, const Point &b)
{
return a.y<b.y;
} double MinDis(int l,int r)
{
if(r-l<1)return INF;
//if(r-l==1)return dis(p[r],p[l]); int m=(l+r)/2;
double d=min(MinDis(l,m),MinDis(m+1,r));
inplace_merge(p+l,p+m+1,p+r+1,cmpy);
int j,right=0;
for(int i=l;i<=r;i++)//0 <n
if(p[i].x >= p[m].x-d && p[i].x<=p[m].x+d)
q[right++]=p[i];
double ret=INF;
for(int i=0;i<right;i++)
{
for(j=i+1;j<right && q[j].y-q[i].y<d;j++)
{
ret=min(ret,dis(q[i],q[j]));
}
}
return min(d,ret);
} int main()
{
double ans; while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
ans=MinDis(0,n-1);
if(ans>=10000)printf("INFINITY\n");
else printf("%.4lf\n",ans);
} return 0;
}
uva 10245 近期点对问题的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- uva 10245 The Closest Pair Problem_枚举
题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- UVA 10245 The Closest Pair Problem【分治】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- UVA - 11354Bond最小生成树,LCA寻找近期公共祖先
看懂题目意思.他的意思是求将全部的城市走一遍,危急度最小.而且给 你两个s,t后让你求在走的时候,从s到t过程中危急度最大的值,并输出它, 然后就是怎样攻克了,这个题目能够说简单,也能够说难 通过思考 ...
- UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
随机推荐
- WASP_百度百科
WASP_百度百科 WASP
- sqlserver安全加固
sqlserver2012安装好以后必要的安全加固,不然非常多DBA的信息普通账户登录后都能够读取到. --use [master] --GO --DENY VIEW SERVER STATE ...
- SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...
- PHP 文件操作类(创建文件并写入) 生成日志
<?php /** * 文件操作(生成日志)支持多条插入 * (假设插入多条语句并换行 用','逗号分开) * */ class log { public $path = './info.txt ...
- hdu 5187 高速幂高速乘法
http://acm.hdu.edu.cn/showproblem.php?pid=5187 Problem Description As one of the most powerful brush ...
- C# 简化优化if/switch 表驱动法
表示这个很强大 字典加反射,搞定多window的switch public partial class MainWindow : Window { Dictionary<string, Type ...
- bat文件无法双击运行
问题: win7系统下新建txt文件,编辑脚本内容后,保存为test.bat.每次双击它,只会默认以txt格式打开它,而不是运行它. 解决: 1. 双击打开“我的电脑”,然后在“工具”下选择“文件夹选 ...
- shell加法
echo 1597+1469+1468+2591+1260+1068+1019+993|bc http://bbs.chinaunix.net/thread-161085-1-1.html http: ...
- Android编程获取网络连接状态及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
- Windows Phone开发(35):使用Express Blend绘图
原文:Windows Phone开发(35):使用Express Blend绘图 上一节中我们简单扯了一下绘图指令,然而那也不是最简单的绘图法,今天,我再向大家推荐一种更好的绘图方案--Express ...