HDU 1007 二维最近点对问题 / 分治
来源:互联网 发布:sony smartband 知乎 编辑:程序博客网 时间:2024/05/22 00:43
发现两两比较的问题都可以用分治办法解决~~~
很多排序都这样~~
HDU 1007
http://acm.hdu.edu.cn/showproblem.php?pid=1007
问题描述:
找到最小的圆半径,使得每次套中的玩具只能是一个。
就是找最近点对的距离,再除2 ;
#include <cstdio>#include <iostream>#include <cmath>#include <algorithm>using namespace std;struct Point{double x , y ;}p[100005];int arr[100005];bool cmpX(const Point& a,const Point& b){return a.x < b.x ;}bool cmpY(const int& a,const int& b){return p[a].y < p[b].y ;}double dis(int a,int b){return sqrt((p[a].x - p[b].x)*(p[a].x - p[b].x) + (p[a].y - p[b].y)*(p[a].y - p[b].y));}double abs(double a,double b){return (a - b > 0) ? (a - b):(b - a);} double find(int l ,int r){if(l+1 == r)return dis(l , r);if(l + 2 == r)return min(dis(l , r) , min(dis(l+1,r) , dis(l , l+1)));int mid = (l + r) >> 1 ;double ans = min(find(l , mid),find(mid+1 ,r)); // 分治 int cnt = 0 ;for(int i = l;i <= r;i++){if(abs(p[i].x , p[mid].x) <= ans)arr[cnt++] = i ; //记录中间的点 }sort(arr , arr + cnt , cmpY);for(int i = 0;i < cnt - 1;i++)for(int j = i + 1;j < cnt;j++){double d = dis(arr[i] , arr[j]);if(d >= ans)break;ans = min(ans , d);}return ans ;}int main(){int n ;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);printf("%.2lf\n",find(0, n-1)/2);}}
0 0
- HDU 1007 二维最近点对问题 / 分治
- hdu--1007(分治求二维最近点对)
- HDU 1007 Quoit Design(最近点对问题:分治)
- 【HDU 1007 】Quoit Design 【分治--最近点对问题】
- hdu 1007 平面最近点对 分治
- hdu 1007 最近点对-分治法
- 分治 hdu 1007 最近点问题
- 二维空间最近点对(分治)
- HDU 1007 Quoit Design(分治法求最近点对问题)
- hdu 1007 Quoit Design (最近点对、分治)
- hdu 1007 经典最近点对 (分治法求解)
- hdu 1007(分治法求最近点对)
- Hdu 1007 - Quoit Design//分治,最近点对,计算几何
- hdu 1007 Quoit Design(分治法求最近点对)
- hdu 1007 Quoit Design(分治求最近点对)
- hdu 1007 Quoit Design 最近点对(分治)
- HDU 1007 Quoit Design 分治法求最近点对
- HDU 1007 Quoit Design 最近点对 分治法
- axis1由wsdl生成客户端和服务端1
- PHP动态柱状图
- MAC上Eclipse安装SVN插件
- 正确使用Block避免Cycle Retain和Crash
- nginx学习之nginx的缓存设置 &提高网站性能
- HDU 1007 二维最近点对问题 / 分治
- (场景)kafka的topic多分区的情况,如何保证跨区的消息消费的顺序性
- 并发服务器思想及编程
- Flash 视频嵌入页面中 覆盖其他层解决办法
- 所谓的迷茫
- Java 读取Excel文件
- 常用shell总结一
- 百度笔试题--论坛数据库表设计
- redis支持数据类型介绍