题目链接:http://acm.swust.edu.cn/problem/794/

Time limit(ms): 1000      Memory limit(kb): 10000
 
Description
设p1=(x1, y1), p2=(x2, y2), …, pn=(xn, yn)是平面上n个点构成的集合S,设计算法找出集合S中距离最近的点对。
 
Input

多组测试数据,第一行为测试数据组数n(0<n≤100),每组测试数据由两个部分构成,第一部分为一个点的个数m(0<m≤1000),紧接着是m行,每行为一个点的坐标x和y,用空格隔开,(0<x,y≤100000)

 
Output

每组测试数据输出一行,为该组数据最近点的距离,保留4为小数。

 
Sample Input
2
2
0 0
0 1
3
0 0
1 1
1 0
 

Sample Output
1.0000
1.0000
Hint
algorithm textbook
 
不想多说,前几天写了一篇博客,主要讲的就是平面最近点对的问题,可以戳戳这里:http://www.cnblogs.com/zyxStar/p/4591897.html
直接上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double inf = 1e20;
const int maxn = ; struct Point{
double x, y;
}point[maxn]; int n, mpt[maxn], t; //以x为基准排序
bool cmpxy(const Point& a, const Point& b){
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
} bool cmpy(const int& a, const int& b){
return point[a].y < point[b].y;
} double min(double a, double b){
return a < b ? a : b;
} double dis(int i, int j){
return sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y));
} double Closest_Pair(int left, int right){
double d = inf;
if (left == right)
return d;
if (left + == right)
return dis(left, right);
int mid = (left + right) >> ;
double d1 = Closest_Pair(left, mid);
double d2 = Closest_Pair(mid + , right);
d = min(d1, d2);
int i, j, k = ;
//分离出宽度为d的区间
for (i = left; i <= right; i++){
if (fabs(point[mid].x - point[i].x) <= d)
mpt[k++] = i;
}
sort(mpt, mpt + k, cmpy);
//线性扫描
for (i = ; i < k; i++){
for (j = i + ; j < k && point[mpt[j]].y - point[mpt[i]].y<d; j++){
double d3 = dis(mpt[i], mpt[j]);
if (d > d3) d = d3;
}
}
return d;
} int main(){
scanf("%d", &t);
while (t--){
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf %lf", &point[i].x, &point[i].y);
sort(point, point + n, cmpxy);
printf("%.4lf\n", Closest_Pair(, n - ));
}
return ;
}

[Swust OJ 794]--最近对问题(分治)的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. 面试题之 query转为obj

    要注意处理编码后的字串  对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...

  2. Windows 取得至高无上的权限

    第一步:gpedit.msc 第二步:计算机配置-->windows 设置 -->安全设置 -->安全选项 -->用户账户控制 -->以管理员批准模式运行所有管理员 -- ...

  3. mysql常见错误码

    1062 - Duplicate entry '1' for key 1 唯一性错误

  4. 让.net程序自动运行在管理员权限下

    原文:让.net程序自动运行在管理员权限下 如何让.net程序自动运行在管理员权限下 VS2010 c# 编译的WINFORM程序 在Win7 以管理员身份运行 windows 7和vista提高的系 ...

  5. mysql 性别存储

    大家在设计数据库时,碰到 性别.状态等 这些 值比较固定的列时,数据类型 是如何定义? 通常都是采用 : 1 create table `XXX` 2 ( 3 ........ 4 sex int(1 ...

  6. Codeforces Gym10008E Harmonious Matrices(高斯消元)

    [题目链接] http://codeforces.com/gym/100008/ [题目大意] 给出 一个n*m的矩阵,要求用0和1填满,使得每个位置和周围四格相加为偶数,要求1的数目尽量多. [题解 ...

  7. Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages

    Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages Web网页中动态数据区域的识别 ...

  8. 【D3.V3.js系列教程】--(十二)坐标尺度

    [D3.V3.js系列教程]--(十二)坐标尺度 1.多种类型的缩放尺度 Quantitative Scales Linear Scales Identity Scales Power Scales ...

  9. 虎记:强大的nth-child(n)伪类选择器玩法

    写在前面的戏: 最近参加了度娘前端IFE的春季班,刷任务,百度真是有营销头脑,让咱们这帮未来的技术狂人为他到处打广告(我可不去哪),其中做的几个任务中有几个以前没有用到的东西, 也算是有些许收获(现在 ...

  10. Studious Student Problem Analysis

    (http://leetcode.com/2011/01/studious-student-problem-analysis.html) You've been given a list of wor ...