问题描述:

源码:

经典问题——最近邻问题,标准解法

#include"iostream"
#include"algorithm"
#include"cmath"
using namespace std; struct Point
{
double x;
double y;
}; Point S[100000];//不使用全局变量可能会超内存 bool cmpPointX(Point a, Point b)
{
return a.x > b.x;
} bool cmpPointY(Point a, Point b)
{
return a.y > b.y;
} double EfficientClosestPair(Point *P, Point *Q, int start, int n)
{
if(n <= 3)
{
double result;
result = (P[start].x - P[start+1].x)*(P[start].x - P[start+1].x) + (P[start].y - P[start+1].y) * (P[start].y - P[start+1].y);
if(n == 3)
{
double tmp = (P[start].x - P[start+2].x)*(P[start].x - P[start+2].x) + (P[start].y - P[start+2].y) * (P[start].y - P[start+2].y);
if(tmp < result)result = tmp;
tmp = (P[start+1].x - P[start+2].x)*(P[start+1].x - P[start+2].x) + (P[start+1].y - P[start+2].y) * (P[start+1].y - P[start+2].y);
if(tmp < result)result = tmp;
}
return result;
}
else
{
int half = n / 2;
double d1 = EfficientClosestPair(P, Q, start, half);
double d2 = EfficientClosestPair(P, Q, start + half, n - half);
double d = d1 < d2 ? d1 : d2;
double m = P[start + half - 1].x;
int count = 0;
double tmp;
for(int i = start; i < start + n; i++)
{
tmp = Q[i].x - m;
if(tmp < 0)tmp = - tmp;
if(tmp < d)count++;
}
double dminsq = d;
if(count > 1)
{
//Point *S = new Point[count];
for(int i = start, j = 0; i < start + n; i++)
{
tmp = Q[i].x - m;
if(tmp < 0)tmp = - tmp;
if(tmp < d)
{
S[j].x = Q[i].x;
S[j].y = Q[i].y;
j++;
}
}
int k;
for(int i = 0; i < count - 1; i++)
{
k = i + 1;
while(k < count && (S[k].y - S[i].y)*(S[k].y - S[i].y) < dminsq)
{
dminsq = min((S[k].x - S[i].x)*(S[k].x - S[i].x) + (S[k].y - S[i].y)*(S[k].y - S[i].y), dminsq);
k++;
}
}
} return dminsq; }
} int main()
{
int n;
Point *P, *Q;
cout.precision(2);
cout.setf(ios::fixed);
P = new Point[100000];
Q = new Point[100000];
while(scanf("%d", &n) != EOF)
{
if(n == 0)break;
for(int i = 0; i < n; i++)
{
scanf("%lf %lf", &P[i].x,&P[i].y);
Q[i].x = P[i].x;
Q[i].y = P[i].y;
}
if(n <= 3)
{
cout<<sqrt(EfficientClosestPair(P, Q, 0, n)) / 2<<endl;
}
else
{
sort(P, P+n, cmpPointX);//使用qsort可能会超时
sort(Q, Q+n, cmpPointY);
cout<<sqrt(EfficientClosestPair(P, Q, 0, n)) / 2<<endl;
}
}
return 0;
}

  

HD-ACM算法专攻系列(15)——Quoit Design的更多相关文章

  1. HD-ACM算法专攻系列(23)——Crixalis's Equipment

    题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的 ...

  2. HD-ACM算法专攻系列(21)——Wooden Sticks

    题目描述: AC源码: 此题考查贪心算法,解题思路:首先使用快速排序,以w或l按升序排序(注意相等时,应按另一值升序排序),这样就将二维变量比较,变为了一维的,排好序的一边就不需要去管了,只需要对未排 ...

  3. HD-ACM算法专攻系列(22)——Max Sum

    问题描述: AC源码: 此题考察动态规划,解题思路:遍历(但有技巧),在于当前i各之和为负数时,直接选择以第i+1个为开头,在于当前i各之和为正数时,第i个可以不用作为开头(因为前i+1个之和一定大于 ...

  4. HD-ACM算法专攻系列(20)——七夕节

    问题描述: AC源码: /**/ #include"iostream" #include"cmath" using namespace std; int mai ...

  5. HD-ACM算法专攻系列(19)——Leftmost Digit

    问题描述: AC源码: 解题关键是,数据很大,不能强算,需要使用技巧,这里使用科学计算法,令N^N=a*10^n ,取对数后变为 N*log10(N)=log10(a)+n,令x = log10(a) ...

  6. HD-ACM算法专攻系列(18)——Largest prime factor

    题目描述: 源码: 需要注意,若使用cin,cout输入输出,会超时. #include"iostream" #include"memory.h" #defin ...

  7. HD-ACM算法专攻系列(17)——find your present (2)

    题目描述: 源码: #include"iostream" #include"string" using namespace std; bool IsFirstH ...

  8. HD-ACM算法专攻系列(16)——考试排名

    问题描述: 源码: 主要要注意输出格式. #include"iostream" #include"iomanip" #include"algorith ...

  9. HD-ACM算法专攻系列(14)——find your present (2)

    问题描述: 源码: #include"iostream" #include"algorithm" using namespace std; bool cmp(i ...

随机推荐

  1. 我的C++笔记(函数部分)

    #include <iostream> #include <cmath>//C++的数学函数库 using namespace std; class Point{ public ...

  2. CSS字体代码

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  3. win系统如何在桌面显示我的电脑

    如果是在Windows Server 2012本地控制台下,直接按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dll,Control_RunDLL desk.c ...

  4. PhotoZoom Classic 7中的新功能

    众所周知PhotoZoom Classic是家庭使用理想的放大图像软件.目前很多用户还在使用PhotoZoom Classic 6,对于PhotoZoom Classic 7还是有点陌生.其实在6代衍 ...

  5. 前端手机验证码cookie存储

    注册的时候经常会有手机验证码的输入这个环节,在第一次点击发送了验证码只后,比如倒计时只走了10秒钟,然后刷新的话,倒计时要还是存在的,这个时候就要有一个cookie的存在了. html的代码 < ...

  6. Django02 Django基础知识

    一.内容回顾 1.web应用程序 2.HTTP协议 a.http协议特性 b.http请求格式 c.http响应格式 3.wsgiref模块 4.Django下载与简单应用 a.Django简介(MT ...

  7. 【转】Oracle基础结构认知——oracle内存结构 礼记八目 2017-12-15 20:31:27

    oracle的数据库实例是一组后台进程和内存结构组成的,而内存结构由系统全局区(system global area)和程序全局区(program global area)组成. #修改SGA和PGA ...

  8. 使用ant design pro搭建项目

    脚手架搭建 git clone --depth=1 https://github.com/ant-design/ant-design-pro.git my-project 然后 cd my-proje ...

  9. HDU 2522 A simple problem( 分数循环节 )

    链接:Here! 思路:模拟除法,当余数再次出现的时候一定是遇到了循环节( 可看下图例子 ),否则的话继续除法的步骤,直到被除数为 0 . 注意:这道题不需要重新申请一个数组来单独存放答案,如果符合要 ...

  10. nyoj314-斐波那契数列四吧

    斐波那契数列四吧 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 斐波那契数列为:0,1,1,2,3,5,8,13....,常规递推公式为f(n)=f(n-1)+f(n- ...