Quoit Design (白话--分治--平面点对问题)

Quoit Design

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.

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

题目描述:
给你很多点,问你最近点的距离一半是多少。
思路典型的求最近平面点对问题 , 可以采用分治法
具体的讲解巨麻烦,还请移步到 –> 分治法编程问题之最接近点对问题的算法分析
AC code:

#include
#include
#include
#include
#include 

using namespace std;
const double maxx=1e50;

int n,t;
struct node{
    double x,y;
}ss[100005];

double min(double u,double v)
{
    return ubool cmp_x(node x,node y) 
{
    return x.xbool cmp_y(node x, node y)
{
    return x.ydouble get_dis(node x,node y){
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
} 

double Merge(int l,int r)  
{
    if(l+1 == r) return get_dis(ss[l],ss[r]); 
    if(l+2 == r) return min(min(get_dis(ss[l],ss[r]),get_dis(ss[l],ss[l+1])),get_dis(ss[l+1],ss[r]));
    int mid = (l+r)>>1;
    double len=min(Merge(l,mid),Merge(mid+1,r));  
    int tmp = 0;node temp[100005];
    for(int i=l;i<=r;i++)
      if(len+ss[mid].x>=ss[i].x && ss[i].x-ss[mid].x<=len)
        temp[++tmp] = ss[i];
    sort(temp+1,temp+tmp+1,cmp_y);
    for(int i=1;i <= tmp;i++)
      for(int j=i+1;j <= tmp;j++)
      {
        if((temp[j].y-temp[i].y)>=len) break;
        len = min(len,get_dis(temp[j],temp[i]));
      }
    return len;
}
int main()
{
    int t;
    while(~scanf("%d",&t) && t) {
        for (int i = 1;i<=t;i++) {
            scanf("%lf %lf",&ss[i].x,&ss[i].y);
        }
        sort(ss+1,ss+1+t,cmp_x);
        double len = Merge(1,t);
        printf("%.2lf\n",len / 2);
    }
    return 0; 
}

但是到了这里我在思考一件事,既然最短距离能够求出,那么最长距离呢? 博主暂时没有解决 To Be continue;

你可能感兴趣的:(分治)