hdu_1007_Quoit Design(最近点对)
题意:
给你平面上的一些点,让你找出这些点的最近点对的距离
题解:
采用分治,达到O(nlognlogn)的时间复杂度就能艹过去了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
//O(nlognlogn)找最近点对
const int N=1e5+;
struct TPoint
{
double x,y;
}ply[N],ans[N];
int n;
inline double MIN(double a,double b) {return a<b?a:b;}
bool cmpx(TPoint a,TPoint b) {return a.x<b.x;}
bool cmpy(TPoint a,TPoint b) {return a.y<b.y;}
double dist(TPoint a,TPoint b)
{
double s1=a.x-b.x;
double t1=a.y-b.y;
return sqrt(s1*s1+t1*t1);
}
double closest(int l,int r)
{
if(l+==r) return dist(ply[l],ply[r]);//2点
else if(l+==r)//三点
return MIN(dist(ply[l],ply[l+]),MIN(dist(ply[l],ply[r]),dist(ply[l+],ply[r])));
int i,j,mid,cnt;
mid=(l+r)>>;
double mi=MIN(closest(l,mid),closest(mid+,r));//递归解决
for(i=l,cnt=;i<=r;i++)//相邻点符合
{
if(fabs(ply[i].x-ply[mid].x)<=mi)
ans[cnt++]=ply[i];
}
sort(ans,ans+cnt,cmpy);//按y排序
for(i=;i<cnt;i++)for(j=i+;j<cnt;j++)//更新最小距离
{
if(ans[j].y-ans[i].y>=mi) break;
mi=MIN(mi,dist(ans[i],ans[j]));
}
return mi;
}
int main()
{
while(scanf("%d",&n),n)
{
int i;
for(i=;i<n;i++) scanf("%lf%lf",&ply[i].x,&ply[i].y);//输入点
sort(ply,ply+n,cmpx);//按x排序
double mi=closest(,n-);
printf("%.2lf\n",mi/);
}
return ;
}
hdu_1007_Quoit Design(最近点对)的更多相关文章
- 杭电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 ...
- HDU 1007Quoit Design(最近点问题)
最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点 思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的. 将区间一半一半的分开,直到分成只有一个点或两个点的时候! 对于只有两 ...
- HDU 1007 Quoit Design最近点对( 分治法)
题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...
- ZOJ2107 Quoit Design 最近点对
ZOJ2107 给定10^5个点,求距离最近的点对的距离. O(n^2)的算法是显而易见的. 可以通过分治优化到O(nlogn) 代码很简单 #include<iostream> #inc ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- Quoit Design(hdu1007)最近点对问题。模版哦!
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- ZOJ 2017 Quoit Design 经典分治!!! 最近点对问题
Quoit Design Time Limit: 5 Seconds Memory Limit: 32768 KB Have you ever played quoit in a playg ...
随机推荐
- C# 在word文档中复制表格并粘帖到下一页中
C# 在word文档中复制表格并粘帖到下一页中 object oMissing = System.Reflection.Missing.Value; Microsoft.Offi ...
- JavaScript JSON timer(计时器) AJAX HTTP请求 同源策略 跨域请求
JSON 介绍 1. JSON: JavaScript Object Notation 是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是 ...
- OBIEE 简介
Oracle BIEE历史: BIEE:全称Oracle Business Intelligence Enterprise Edition. Oracle公司在05年底收购Siebel,取其前端开发工 ...
- 使用非 GUI 模式运行 JMeter 压力测试
使用非 GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源.使用命令jmeter -n -t <testplan filename> -l <list ...
- WebDriver使用IE和chrome浏览器
因为我用的是 selenium-dotnet-2.47.0.zip IEDriverServer_x64下载地址 https://code.google.com/p/selenium/download ...
- js 系统时间对象
alert(new Date()); 年是多少,月,日 var year=new Date(); var m=year.Getfullmonth; alert(m);月份特殊,必须加个数字1 数据类型 ...
- OpenCV使用边缘提取、腐蚀、轮廓进行车牌定位
http://blog.csdn.net/superdont/article/details/24935383 OpenCV使用边缘提取.腐蚀.轮廓进行车牌定位 2014-05-03 21:38 67 ...
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 题意:给你一个集合,让你求递增子序列有多少个,和树状数组求逆序对差不多,不过数据比较大,要离散化 ...
- LeetCode OJ 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- android studio导入包后无法import
android studio导入jar包的方法: 1.将jar包放到module的libs目录下 2.在所导入的jar包上右键,选择“Add as library”. 其中,第二点跟eclipse不同 ...