HDU 1007 最近点对问题 Quoit Design(分治+贪心)

HDU 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

题意:

题意为有很多点让你找出距离最近的点,也就是经典的最近点问题。

最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点

思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的/    将区间一半一半的分开,直到分成只有一个点或两个点的时候!
 对于只有两个点的区间,最小值就是这两个点的距离,只有一个点的区间,
 最小值就是无穷大。注意还要考虑合并的时候,可能距离最近的两个点,
分别在左右两个不同的区间。对于这种情况的处理如下:
      mid=(ld+rd)/2;
      ans = min(QuoitDesigne(ld, mid), QuoitDesigne(mid+1, rd));得到两段区间最小值的最小值
     从中间向两边寻找,因为我们是按照x坐标排序的,在左区间向左边寻找的时候
    如果某一个点的x到中间点x的距离大于ans(否则将这样的点保存),那么这个
    点左边的点就不可能在右区间寻找到相应的点满足两个点的距离小于ans的,那么
   就结束继续查找(这样算是一种优化)

    同理在右区间向右寻找。。。

     然后对存储的节点按照y坐标进行从小到大的排序。
    枚举每两个点寻找最小的距离
//      最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点

//     思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的。
//     将区间一半一半的分开,直到分成只有一个点或两个点的时候!
//     对于只有两个点的区间,最小值就是这两个点的距离,只有一个点的区间,
//     最小值就是无穷大。注意还要考虑合并的时候,可能距离最近的两个点,
//     分别在左右两个不同的区间。对于这种情况的处理如下:
//          mid=(ld+rd)/2;
//          ans = min(QuoitDesigne(ld, mid), QuoitDesignee(mid+1, rd));得到两段区间最小值的最小值
//         从中间向两边寻找,因为我们是按照x坐标排序的,在左区间向左边寻找的时候
//         如果某一个点的x到中间点x的距离大于ans(否则将这样的点保存),那么这个
//         点左边的点就不可能在右区间寻找到相应的点满足两个点的距离小于ans的,那么
//         就结束继续查找(这样算是一种优化)

//         同理在右区间向右寻找。。。

//         然后对存储的节点按照y坐标进行从小到大的排序。
//         枚举每两个点寻找最小的距离
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;
#define FASTIO                   \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define x first
#define y second
const int N = 1e5 + 5;
const double MAX = 9999999999999.0;
typedef pair<double, double> PII;

PII nd[100005], ndx[100005];
bool cmp(PII a, PII b)
{
    if (a.x == b.x)
        return a.y<b.y;
    return a.x < b.x;
}
bool cmpy(PII a, PII b)
{
    return a.y < b.y;
}
double dist(PII a, PII b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double QuoitDesigne(int ld, int rd)
{
    if (ld == rd)
        return MAX;
    if (ld + 1 == rd)
        return dist(nd[ld], nd[rd]);
    int mid = (ld + rd) / 2;
    double ans = min(QuoitDesigne(ld, mid), QuoitDesigne(mid + 1, rd));
    int len = 0;
    for (int i = mid; i >= ld; --i)
        if (nd[mid].x - nd[i].x <= ans)
            ndx[len++] = nd[i];
        else
            break;
    for (int i = mid + 1; i <= rd; ++i)
        if (nd[i].x - nd[mid].x <= ans)
            ndx[len++] = nd[i];
        else
            break;

    sort(ndx, ndx + len, cmpy);
    for (int i = 0; i < len - 1; ++i)
        for (int j = i + 1; j < len; ++j)
            if (ndx[j].y - ndx[i].y >= ans)
                break; 
            else
                ans = min(ans, dist(ndx[i], ndx[j]));
    return ans;
}
int main()
{
    FASTIO;
    int n;
    while (cin >> n && n)
    {
        for (int i = 0; i < n; i++)
            cin >> nd[i].x >> nd[i].y;
        sort(nd, nd + n, cmp);
        double ans = QuoitDesigne(0, n - 1) / 2.0;
        cout << setiosflags(ios::fixed) << setprecision(2) << ans << endl;
    }
   
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值