首页 > ACM题库 > HDU-杭电 > HDU 1007 Quoit Design-分治-[解题报告] C++
2013
11-26

HDU 1007 Quoit Design-分治-[解题报告] C++

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 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.

输入:

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.

输出:

For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

样例输入:

2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0

样例输出:

0.71
0.00
0.75

本题题目很裸,只要求出平面中最近点对的距离,然后除以2就是答案,方法就是分治,具体的可以参见算法导论

简单分析:对于该问题的一个子问题,只需把当前点的集合再分成两部分,分的方法是:按照x(或者y)排序,然后分成了前一半和后一半,即相当于在这些点的中间做了一条垂线,然后算垂线左边的最近点对(设距离为d1),垂线右边的最近点对(设距离为d2),那么在当前情况下,最近点对可能出自左半部分的子问题,也可能是右半部分,还可能是左边一个点,右边一个点的那种,对于前两种情况,已经算得d1,d2,比个大小就好了,对于第三种情况,则需再算,设d=min(d1,d2),那么第三种情况除非能找到小于d的点对,不然是没有意义的。如果左右两方的点对要小于d,那么前提就是左边的点到那条垂线(刚才分左右的那条线)的距离小于d,右边同理(这个很容易想象)……这样一来,点的数量就会大大减少……当然光这样也是不够的。我们需要对左边那部分的点的Y坐标排序,对右边也是,但是这里用不着每次都O(nlogn)的去快排一次,只要在最初的时候把所有的点快排完,存在数组Y中,然后每次左右分完之后,只需按X的顺序扫一遍相应的Y数组,如果扫到的点在左办边,就把Y[i]赋值到左半边,右边亦然,这样肯定能保证,左右两半的Y都是有序的。取出Y中距离垂线距离小于d的所有点,然后对这些点暴力,因为Y是有序的,所以可以有效利用这个条件,每个点只要和之后的5个点算一下就好了,为什么呢?

假设当前扫到了点P(假设在左边),那么点P以下的点其实都不用考虑了,因为已经算过了,对点P作这样一个2d*d的矩形,如果要找到与点P距离小于d的点,那么右边那个点必须落在右边那个d*d的矩形中……同理当P在右边时也成立。又左半边所有的点的距离不小于d,所以左半边那个d*d的矩形中最多放4个点(这个很直观,当然也需要证明过,在此详细说了);右边亦然…………而如果真的要做到的话,垂线上必须放两个,即左右有重复算的(这在分完左右两边之后是不会发生的,因为右边的点其实不会在垂线上)…………总之,不严密的可以看出,对P可能形成最近点对的点最多只有他后面的5个(前面的点之前已经算完了)…………这样对每个点枚举五次就好了…………

以下是代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define esp 1e-6
using namespace std;
struct point
{
    double x,y;
}p[100010];
int px[100010],py[100010],ty[100010];
int cmpx(const point &a,const point &b)
{
    return a.y<b.y;
}
int cmpy(const int &a,const int &b)
{
    return p[a].x<p[b].x;
}
inline double min(double a,double b)
{
    return a<b?a:b;
}
inline double dist2(point &a,point &b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double min_dis = 1e100;
double mindist(int* X,int *Y,int size)
{
    if(size<=3)
    {
        if(size==2)
        return dist2(p[X[0]],p[X[1]]);
        for(int i=0; i<size; i++)
        min_dis = min(min_dis,dist2(p[X[i]],p[X[(i+1)%size]]));
        return min_dis;
    }
    int pl = (size+1)/2;
    int pr = size - pl;
    int l1 = 0,l2=pl;
    for(int i=0; i<size; i++)
    if(Y[i]<X[pl])
    ty[l1++] = Y[i];
    else ty[l2++] = Y[i];
    for(int i=0; i<size; i++)
    Y[i] = ty[i];
    min_dis = min(mindist(&X[0],&Y[0],pl),mindist(&X[pl],&Y[pl],pr));
    l1 = 0;
    for(int i=0; i<size; i++)
    if((p[Y[i]].x-p[X[pl-1]].x)*(p[Y[i]].x-p[X[pl-1]].x)<=min_dis)
    ty[l1++] = Y[i];
    for(int i=0; i<l1; i++)
    for(int j=1; j<6&&i+j<l1; j++)
    if((ty[i]-X[pl])*(ty[i+j]-X[pl])<=0)
    min_dis = min(min_dis,dist2(p[ty[i]],p[ty[i+j]]));
    return min_dis;
}

int main()
{
    int n;
    for(int i=0; i<100010; i++)
    px[i] = i;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            py[i] = i;
        }
        sort(p,p+n,cmpx);
        sort(py,py+n,cmpy);
        min_dis = 1e100;
        printf("%.2lf\n",sqrt(mindist(px,py,n))/2);
    }
    return 0;
}


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }