Quoit Design 分治法的一个题

目前对于分治法的理解还不是很透彻,今天接触了这样一个题,不太明白,看了网上的解析后,自己总结一下.

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

题意:

大家都玩过套圈吧,套圈的时候是不是一次只能套中一个呢?而且很多时候套不中.原因在于(题意):在地上的所有物品两两之间都有距离,其中一定存在一个最小距离,只要圈的直径能够保证与这个距离相等(实际生活中要小),就可以保证一次套圈只能套中一个.

分析:

那么问题来了,怎么求这个最小距离呢,大家一定想到的是,找到一个点与其他所有点进行比较,再找第二个点在进行比较...直到遍历所有点.暴力枚举法.一定会时间超限的.那么如何才能够使得时间复杂度降低呢?

首先想一下二位平面内最短距离如何构成:X轴,Y轴 距离共同决定了两点距离.

我们可不可以按照xy分别求一下呢?比如x轴求出最短距离,y轴求出最短距离.但是问题又来了,这两个最短距离不一定属于同两个点坐标的差值(很大程度上不是).咋整?

我们可以先利用分治思想(1)先按照x轴进行排序,得到所有点的从小到大排序序列.然后对这个序列进行操作:最关键的一步,时间复杂度如何降低:(2)利用二分思想,将整个序列分成两部分,对于每一个部分分别求出最小距离得到了d1和d2(这里运用了递归),分成两个部分以后,两边的分界点之间的距离有可能是最小的d3,同时要考虑到d3也有可能是两个部分之间其他点之间的距离.所以要将与最小值相近的点开辟数组进行存储.

这里为什么会降低时间复杂度呢?首先暴力枚举是:n*(n-1)+(n-1)*(n-2)......而二分则是将空间分成两部分在两部分中分别寻找最优解,同时再将整个空间与找到的最优解相比较.找到最优解区间.这个过程中时间复杂度降低.

(3)将数组中存储的点,按照y轴进行从小到大排序,最终输出结果.

代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct P
{
    double x;
    double y;
} point[100001];
long long int n,agn[100001];
double Min(double a,double b)
{
    return a<b?a:b;
}
bool cmpx(P a,P b)
{
    return a.x<b.x;
}
bool cmpy(int a,int b)
{
    return point[a].y<point[b].y;
}
double dis(P a,P b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Short(int left,int right)
{
    if(right==left+1)
        return dis(point[right],point[left]);
    else if(right==left+2)
        return Min(dis(point[left],point[right]),Min(dis(point[left],point[left+1]),dis(point[left+1],point[right])));
    int mid=(left+right)/2;
    double ans=Min(Short(left,mid),Short(mid,right));
    int cnt=0;
    for(int i=left; i<=right; i++)
    {
        if(point[i].x>=point[mid].x-ans && point[i].x<=point[mid].x+ans)
            agn[cnt++]=i;
    }
    sort(agn,agn+cnt,cmpy);
    for(int i=0; i<cnt; i++)
        for(int j=i+1; j<cnt; j++)
        {
            if(point[agn[j]].y-point[agn[i]].y>=ans)
                break;
            ans=Min(ans,dis(point[agn[i]],point[agn[j]]));
        }
    return ans;
}
int main()
{
    while(scanf("%lld",&n)!=EOF)
    {
        double sum=0.0;
        if(n==0)
            break;
        for(int i=0; i<n; i++)
        {
            scanf("%lf %lf",&point[i].x,&point[i].y);

        }
        sort(point,point+n,cmpx);
        cout<<fixed<<setprecision(2)<<Short(0,n-1)/2<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值