hdu 1007 Quoit Design, 平面最近点对

平面最近点对的经典做法就是分治。所有点先按x再按y排序,然后取中间位置的点,利用其x坐标值将点划分成左右两部分,分别求出两侧的最近点对(设其距离为δ),然后合并。参看下面的图片(来自guyulongcs的专栏,http://blog.csdn.net/guyulongcs/article/details/6841550)合并的时候需要注意,大致有两种做法,一种是把分界线两侧δ范围内的点分别加进一个数组,然后对右侧数组按y坐标值排序,之后依次为每个左侧数组中的点,在右侧数组中寻找与其y坐标值最近的6个点,并计算距离;另一种做法是,将分界线两侧δ范围内的点全部加进一个数组,然后按y坐标排序,每个点与其后面的8个点作比较,求出最近距离。理论上第一种方法更快一些,此处给出第一种 方法实现的代码:

图片来源:http://images.cnblogs.com/cnblogs_com/king1302217/%E5%88%86%E6%B2%BB%E5%88%92%E5%88%86_1.jpg

方法1代码:

#include  <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
#define N 100100
struct point{
    double x, y;
    bool operator < (const point &a) const{
        if(x == a.x) return y < a.y;
        return x < a.x;
    }
}p[N], pl[N], pr[N];

bool compy(point a, point b){
    return a.y < b.y;
}

double dist(point a, point b)
{
    double x = a.x-b.x, y = a.y-b.y;
    return x*x+y*y;
}
int fd(int l, int r, int key)
{
    while(l < r-1)
    {
        int mid = l+r>>1;
        if(pr[mid].y < key) l = mid+1;
        else r = mid;
    }
    return l;
}

double solve(int l, int r)
{
    if(l == r) return 1.0*inf;
    else if(l+1 == r) return dist(p[l], p[r]);
    int mid = l+r>>1;
    double d1 = solve(l, mid);
    double d2 = solve(mid+1, r);
    double d = min(d1, d2);
    int cl = 0, cr = 0;
    for(int i = l; i <= mid; i++)
        if(p[mid].x - p[i].x <= d)
            pl[cl++] = p[i];
    for(int i = mid+1; i <= r; i++)
        if(p[i].x - p[mid].x <= d)
            pr[cr++] = p[i];
    sort(pr, pr+cr, compy);
    for(int i = 0; i < cl; i++)
    {
        int id = fd(0,cr-1, pl[i].y-d);
        for(int j = 0; j < 6 && id+j < cr; j++)
            d = min(d, dist(pl[i], pr[id+j]));
    }
    return d;
}
int main()
{
    int n;
    while(~scanf("%d", &n), n)
    {
        for(int i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y);
        sort(p, p+n);
        double ans = solve(0, n-1);
        printf("%.2lf\n", sqrt(ans)/2.0);
    }
    return 0;
} 
时间: 2024-08-14 04:05:24

hdu 1007 Quoit Design, 平面最近点对的相关文章

HDU 1007 Quoit Design (最近点对 分治法)

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 ca

HDU 1007 Quoit Design | 平面分治

暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define N 100010 using namespace std; void chkmin(double x,double y) {if (x>y) x=y;} int n; struct point { double x,y; point(){}; point(double _x,double _

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 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 ri

HDU 1007 Quoit Design【计算几何/分治/最近点对】

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 58566    Accepted Submission(s): 15511 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 Quoit Design

Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 130 Accepted Submission(s): 72   Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings a

分治 [HDU 1007] Quoit Design

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33577    Accepted Submission(s): 8800 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 Quoit Design(二分+浮点数精度控制)

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 47104    Accepted Submission(s): 12318 Problem Description Have you ever played quoit in a playground? Quoit is a game in which fla

hdu 1007 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 carefull