37. 套圈

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.

 测试输入 期待的输出 时间限制 内存限制 额外进程
测试用例 1以文本方式显示
  1. 4↵
  2. 0 3↵
  3. 3 2↵
  4. 4 0↵
  5. 7 1↵
  6. 0↵
以文本方式显示
  1. 1.12↵
1秒64M0

code

  1. 其实就是最近点对的问题,有很多点,用环来套,让这个环半径最大,但是不能套到两个点,那么很明显最近点的距离的一半就是他的半径
  2. 最近一直在看最近点对,这是我在网上找到的一篇,排序放到外面,但是这个排序的逻辑我没有看懂,为什么使用同一个数组也可以,而且对中间部分的处理也是所有的点都处理,运行速度还不慢,很疑惑
  3. 代码:
/******************************************
 * @Author       : 鱼香肉丝没有鱼
 * @Date         : 2021-09-20 12:55:54
 * @LastEditors  : 鱼香肉丝没有鱼
 * @LastEditTime : 2021-12-10 09:44:18
 ******************************************/

// https://www.zsdocx.com/p-3399159.html
//排序放到分治外面

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <iostream>

using namespace std;

#define N 2000000
const double inf = 5e18;

typedef struct
{
    double x;
    double y;
} Point;

int cmpx(const void* a, const void* b) {
    return ((Point*)a)->x - ((Point*)b)->x;
}

int cmpy(const void* a, const void* b) {
    return ((Point*)a)->y - ((Point*)b)->y;
}

double spow(const double& a) {
    return a * a;
}

double dist(Point a, Point b) {
    return sqrt(spow(a.x - b.x) + spow(a.y - b.y));
}

double Nearest(Point* p, int n) {
    double d1, d2, d3, dd, min_d;
    double left_d, right_d, d;
    int i, j;
    if(n == 1)
        return inf;
    if(n == 2)
        return dist(p[0], p[1]);
    if(n == 3) {
        d1 = dist(p[0], p[1]);
        d2 = dist(p[1], p[2]);
        d3 = dist(p[2], p[3]);
        return d3 < ((d1 < d2) ? d1 : d2) ? d3 : ((d1 < d2) ? d1 : d2);
    }

    left_d = Nearest(p, n / 2);
    right_d = Nearest(&p[n / 2], n - n / 2);
    d = left_d < right_d ? left_d : right_d;

    min_d = d;
    for(i = 0; i < n / 2; i++)  //左边部分
        for(j = n / 2; j < n && fabs(p[j].y - p[i].y) < d; j++) {  //右边部分
            dd = dist(p[i], p[j]);
            min_d = min_d < dd ? min_d : dd;
        }

    return min_d;
}

int main() {
    // clock_t start, end;
    // double total;
    // start = clock();

    int i;
    int n = 0;
    double dis = 0;

    Point* p = NULL;
    // freopen("test data.in", "r", stdin);
    // freopen("file in.txt", "r", stdin);
    // freopen("file.txt", "r", stdin);
    scanf("%d", &n);

    while(n) {
        p = new Point[n];

        for(i = 0; i < n; i++) {
            scanf("%lf %lf", &p[i].x, &p[i].y);
        }

        qsort(p, n, sizeof(Point), cmpx);
        qsort(p, n / 2, sizeof(Point), cmpy);
        qsort(p + n / 2, n - n / 2, sizeof(Point), cmpy);

        dis = Nearest(p, n);

        printf("%.2lf\n", dis / 2);

        delete[] p;

        scanf("%d", &n);
    }

    // end = clock();
    // total = (end - start) / CLOCKS_PER_SEC;
    // cout << total << endl;

    return 0;
}

/*
4
0 3
3 2
4 0
3 2
0
*/

summary

  1. 学习到了引用的另一个神奇的地方
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值