题意:
求最近点对的距离。
思路:
二维平面上的分治, 很显然 先将点按照x坐标排序。
答案来自三部分, 点对完全在左边,完全在右边, 和一个在左一个在右的合并过程。
简单说一下合并部分。
一般来说 左边完全匹配右边, 来更新答案。但复杂度略高。
可以加一些剪枝。
比如 把mid 点附近ans 距离的点存下来。
从这些点里匹配。
在根据y 坐标剪枝。(先按y坐标排序)
如果两个点 距离超过了ans 直接break了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
const int maxn = 100000 + 10;
struct node{
double x, y;
node(double x = 0, double y = 0):x(x), y(y){}
void read(){
scanf("%lf %lf",&x, &y);
}
}p[maxn], a[maxn];
bool cmp_x(node a, node b){
return a.x < b.x;
}
bool cmp_y(node a, node b){
return a.y < b.y;
}
double get(node a, node b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double dfs(int l,int r){
if (l == r) return 1e18;
if (l + 1 == r){
return get(p[l], p[r]);
}
int m = l + r >> 1;
double ans = min(dfs(l, m), dfs(m+1, r));
int cnt = 0;
for (int i = l; i <= r; ++i){
if (p[i].x >= p[m].x - ans && p[i].x <= p[m].x + ans){
a[cnt++] = p[i];
}
}
sort(a, a+cnt, cmp_y);
for (int i = 0; i < cnt - 1; ++i){
for (int j = i+1; j < cnt; ++j){
if (a[j].y - a[i].y > ans) break;
ans = min(ans, get(a[i], a[j]));
}
}
return ans;
}
int main(){
int n;
while(~scanf("%d",&n) && n){
for (int i = 0; i < n; ++i){
p[i].read();
}
sort(p, p + n, cmp_x);
printf("%.2f\n", dfs(0, n-1) / 2);
}
return 0;
}
Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54714 Accepted Submission(s): 14411
Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
Sample Output
0.71 0.00 0.75
Author
CHEN, Yue
Source
Recommend
JGShining