[Swust OJ 794]--最近对问题(分治)

题目链接: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

直接上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 const double inf = 1e20;
 8 const int maxn = 100005;
 9
10 struct Point{
11     double x, y;
12 }point[maxn];
13
14 int n, mpt[maxn], t;
15
16 //以x为基准排序
17 bool cmpxy(const Point& a, const Point& b){
18     if (a.x != b.x)
19         return a.x < b.x;
20     return a.y < b.y;
21 }
22
23 bool cmpy(const int& a, const int& b){
24     return point[a].y < point[b].y;
25 }
26
27 double min(double a, double b){
28     return a < b ? a : b;
29 }
30
31 double dis(int i, int j){
32     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));
33 }
34
35 double Closest_Pair(int left, int right){
36     double d = inf;
37     if (left == right)
38         return d;
39     if (left + 1 == right)
40         return dis(left, right);
41     int mid = (left + right) >> 1;
42     double d1 = Closest_Pair(left, mid);
43     double d2 = Closest_Pair(mid + 1, right);
44     d = min(d1, d2);
45     int i, j, k = 0;
46     //分离出宽度为d的区间
47     for (i = left; i <= right; i++){
48         if (fabs(point[mid].x - point[i].x) <= d)
49             mpt[k++] = i;
50     }
51     sort(mpt, mpt + k, cmpy);
52     //线性扫描
53     for (i = 0; i < k; i++){
54         for (j = i + 1; j < k && point[mpt[j]].y - point[mpt[i]].y<d; j++){
55             double d3 = dis(mpt[i], mpt[j]);
56             if (d > d3)    d = d3;
57         }
58     }
59     return d;
60 }
61
62 int main(){
63     scanf("%d", &t);
64     while (t--){
65         scanf("%d", &n);
66         for (int i = 0; i < n; i++)
67             scanf("%lf %lf", &point[i].x, &point[i].y);
68         sort(point, point + n, cmpxy);
69         printf("%.4lf\n", Closest_Pair(0, n - 1));
70     }
71     return 0;
72 }

时间: 2024-08-26 08:14:19

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

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

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

题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ (2+3))=((5)+(5))=10.除去原数不4,1,2,3之外,其余都为中间结果,如5,5,10,将中间结果相加

swust oj 1126--神奇的矩阵(BFS,预处理,打表)

题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈特13成功解决了填数矩阵问题.不知道他这一周又从哪儿弄来一神奇的矩阵,于是逃课潜心研究了一周,终于发现了其中的奥秘:该矩阵有2行.4列,即8个小方块,每个小方块上有一个数字,即:1 2 3 45 6 7 8对于这个神奇的矩阵,有3种变换方式,具体如下:变换A:上下两行数字互换,如上图可变为:5 6

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

题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 哈特13最近在学习数论问题,然后他智商太低,并学不懂.这不,他又碰到不会的题了.题意非常简单: 有n个数字,求出这些数字中两两最大公约数的最大值.你一定要帮助他解决这个问题啊. Input 多组输入,约25组,直到文件末尾.每组数据占2行,第一行为数字个数n,2<=n<=100000第二行即为对应

C语言BFS(5)___TT与魔法师(swust oj 2464)

Description TT生活在一个充满魔法的国度,为了便于管理,国王请魔法师在一些重要的城市之间造出了"彩虹桥"!彩虹桥的特殊之处在于,可以从桥的一头瞬间移动到另一头.国王还请魔法师为彩虹桥设计出了通行证,通行证大致分成A,B,C三种,彩虹桥也对应A,B,C三种,每个彩虹桥可以识别的通行证最多为三种,每个人都拥有一个唯一类型的通行证,拥有通行证的人可以在对应的彩虹桥两头来回穿梭,如拥有A通行证的人只可以穿梭于可识别A通行证的彩虹桥. 一天,TT因为有急事需要从城市1穿梭到城市N,显