HDOJ-1007 Quoit Design(最近点对问题)
http://acm.hdu.edu.cn/showproblem.php?pid=1007
给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具
实际就是要求最近的两个坐标的距离
典型的最近点对问题
最近点对详解
http://blog.csdn.net/lonelycatcher/article/details/7973046
//最近点对
# include <stdio.h>
# include <algorithm>
# include <math.h>
# define MAX 100005
# define INF 100000000
using namespace std; struct POINT
{
double x, y;
}arr[MAX]; int n, temp[MAX]; bool cmp(const POINT& a, const POINT& b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
} bool cmp2(const int& a, const int& b)
{
return arr[a].y < arr[b].y;
} double dis(int a, int b)
{
return sqrt(pow(arr[a].x - arr[b].x, 2) + pow(arr[a].y - arr[b].y, 2));
} double min(double a, double b)
{
return a<b?a:b;
} double closest_pointpair(int left, int right)
{
double d = INF; if(left == right)//单个点不计算直接返回INF
return d;
if(left + 1 == right)//相邻点直接计算距离
return dis(left, right); int mid = (left + right) / 2;//取区间中值
d = min(closest_pointpair(left, mid), closest_pointpair(mid+1, right));//分治 取二者中的小数作为领域范围 int num = 0;
for(int i = left; i <= right; i++)//记录中值两边+(-)d范围内的所有点的下标
if(fabs(arr[mid].x - arr[i].x) <= d)
temp[num++] = i; sort(temp, temp+num, cmp2);//按y从小到大排序 for(int i = 0; i < num; i++)//两两枚举 若距离小于d 替换
{
for(int j = i + 1; j < num && arr[temp[j]].y - arr[temp[i]].y < d; j++)
{
double dt = dis(temp[i], temp[j]);
d = min(d, dt);
}
} return d;//返回当前分域最近点对距离
} int main()
{
while(scanf("%d", &n) && n)
{
for(int i = 0; i < n; i++)
scanf("%lf %lf",&arr[i].x, &arr[i].y); sort(arr, arr+n, cmp); printf("%.2lf\n", closest_pointpair(0, n-1) / 2);
} return 0;
}
HDOJ-1007 Quoit Design(最近点对问题)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Hdoj 1007 Quoit Design 题解
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- HDU 1007 Quoit Design最近点对( 分治法)
题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...
- 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【计算几何/分治/最近点对】
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 ...
- poj 1007 Quoit Design(分治)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- [Ubuntu]在Ubuntu下搭建自己的源服务器
1.摘要 网上有很很多关于搭建源镜像的文章,但是对于一般人来讲,用不着镜像所有的deb包,只对我们能用到的感兴趣.另外,对于一些在Ubuntu的源中没有的软件,我们也可以放在我们自己的源里,这 ...
- JS Message 网页消息提醒
JS message是一个非常小的(用gzip压缩之后才3kb)JavaScript library 用于轻松在网页上展示通知提醒.除了通知,它还支持创建带风格的对话框和确认对话框.不需要任何JS框架 ...
- cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- ChromiumFX中js调用C#方法
server端代码: ChromiumWebBrowser wb; wb.AddGlobalJSFunction("CfxHelloWorld").Execute += CfxHe ...
- C++中如何建立一个顺序表
准备数据 #define MAXLEN 100 //定义顺序表的最大长度 struct DATA { char key[10]; //结点的关键字 char name[20]; int age; }; ...
- jQuery——AJAX(一)
jQuery.ajax(options):通过HTTP请求加载远程数据, 参数:options(可选),ajax请求设置.所有选项都是可选的. 返回值:XMLHttpRequest function ...
- WCF Rest:不使用UriTemplate使用post方式传参解决HTTP400问题以及参数映射问题
在使用POST方式向服务提交数据时,出现HTTP400异常,以下代码描述: 服务接口定义: [OperationContract] [WebInvoke(ResponseFormat = WebMes ...
- Information seeking letter, hard copy version
23 Roanoke Street Blacksburg, VA 24060 (540) 555-1123 K.Walker@vt.edu October 23, 20XY Mr. James G. ...
- Godaddy主机从购买到开通的详细图文教程(2013年)
http://bbs.zhujiusa.com/thread-10-1-1.html Godaddy主机从购买到开通的详细图文教程(2013年最新) Godaddy是全球域名注册商中的NO.1,同时也 ...
- OCMOCM
14年,OCM考试费12000 15年,考试费19800 对于我来说,1,2年之后是否换工作还是个未知数 在本单位考这个貌似没什么用处,工资也不会突然就涨很多 跳槽的话,专门做数据库感觉压力还挺大 年 ...