2. 套圈

题干

        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 (could be decimal). 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

翻译

        你曾经在操场上玩过quoit吗?Quoit是一种向一些玩具投掷扁平圆环的游戏,所有被包围的玩具都会获得奖励。在Cyberground领域,每个玩具的位置都是固定的,圆环经过精心设计,一次只能包围一个玩具。另一方面,为了让游戏看起来更有吸引力,环被设计成具有最大的半径。给定一个场的配置,你应该找到这样一个环的半径。假设所有的玩具都是平面上的点。如果一个点和环的中心之间的距离严格小于环的半径,则该点被环包围。如果两个玩具放在同一点上,则认为环的半径为0。
        输入输入由几个测试用例组成。对于每种情况,第一行都包含一个整数N(2<=N<=100000),即字段中玩具的总数。接下来是N行,每行包含一对(x,y),它们是玩具的坐标(可以是十进制)。输入以0终止。
 

        输出对于每个测试用例,在一行中打印Cyberground管理器所需的环的半径,精确到小数点后2位

        简言之,求输入数据对所构成平面点集中,距离最近点间距离的一半(半径R)。

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

思路: 

        分治法就不说了,这次尝试了一下非分治算法,据说时间复杂度和分治法一样,但是不知为什么死在第二个测试用例上了,先把错误的代码交上来嘿嘿

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <set>
const int N = 200005;
double ans = 1e20;

using namespace std;

struct point
{
    double x, y;
    point(double x = 0, double y = 0) : x(x), y(y) {}
};

struct cmp_x
{
    bool operator()(const point &a, const point &b) const
    {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
};

struct cmp_y
{
    bool operator()(const point &a, const point &b) const { return a.y < b.y; }
};

void upd_ans(const point &a, const point &b)
{
    double dist = sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
    if (ans > dist)
        ans = dist;
}

point a[N];
multiset<point, cmp_y> s;

 
int main()
{
    int n;
    while (cin >> n && n != 0)
    {
        s.clear();
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &a[i].x, &a[i].y);
        sort(a, a + n, cmp_x());
        for (int i = 0, l = 0; i < n; i++)
        {
            while (l < i && a[i].x - a[l].x >= ans)
                s.erase(s.find(a[l++]));
            for (auto it = s.lower_bound(point(a[i].x, a[i].y - ans));
                 it != s.end() && it->y - a[i].y < ans; it++)
                upd_ans(*it, a[i]);
            s.insert(a[i]);
        }
        printf("%.2lf\n", ans / 2.0);
    }
    return 0;
}

  • 28
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的OpenMV程序示例,使用机器视觉模块循迹,使小车在操场上沿着白线前进。 ```python import sensor import image import time import pyb # 初始化OpenMV摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) # 初始化电机 left_motor = pyb.Pin("P0", pyb.Pin.OUT_PP) right_motor = pyb.Pin("P1", pyb.Pin.OUT_PP) # 定义循迹阈值 threshold = (200, 255) # 阈值 # 检测白色线条 def detect_white_line(img): # 进行二值化处理 img.binary([threshold]) # 提取线段 lines = img.find_lines(threshold=1000, theta_margin=25, rho_margin=25) # 获取最长线段 max_len = 0 max_line = None for line in lines: if line.length() > max_len: max_len = line.length() max_line = line return max_line # 跟踪白色线条 while True: img = sensor.snapshot() # 获取图像 line = detect_white_line(img) # 检测白色线条 if line: # 计算线段的中心点 cx = line.x1() + (line.x2() - line.x1()) // 2 cy = line.y1() + (line.y2() - line.y1()) // 2 # 根据中心点位置控制电机 if cx < 150: left_motor.high() right_motor.low() elif cx > 170: left_motor.low() right_motor.high() else: left_motor.high() right_motor.high() else: # 没有检测到线条,停止电机 left_motor.low() right_motor.low() time.sleep(10) # 等待10毫秒 ``` 该程序使用机器视觉模块检测白色线条,并根据线条的位置控制电机前进,实现在操场上循白线前进的功能。该程序只是OpenMV的一个简单示例,实际使用中需要根据具体需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值