当前位置: 首页 > news >正文

【模板】平面最近点对

【模板】平面最近点对

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
constexpr int N = 1e6 + 10;
struct dot {int x, y;
} a[N];
double dist(dot a, dot b) {return hypot(a.x - b.x, a.y - b.y);
}
int n;
double ans;
multimap<int, int, less<>> mp;
multimap<int, int, less<>>::iterator its[N];
int main() {
#ifndef LOCALcin.tie(nullptr)->sync_with_stdio(false);  
#endifcin >> n;for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;sort(a + 1, a + n + 1, [&](dot lhs, dot rhs) { return lhs.x < rhs.x; });ans = dist(a[1], a[2]), ans2 = dist2(a[1], a[2]);for (int i = 1, l = 1; i <= n; i++) {while (l < i && a[i].x - a[l].x >= ans) mp.erase(its[l++]);auto itl = mp.upper_bound(a[i].y - ans);auto itr = mp.lower_bound(a[i].y + ans);auto vec = vector<pair<int, int>>(itl, itr);for (auto [y, x] : vec) ans = min(ans, dist(a[i], {x, y}));its[i] = mp.insert({a[i].y, a[i].x});}cout << fixed << setprecision(4) << ans << endl;return 0;
}

multimap<int, int, less<>> 中的 less<> 是必要的。然后这种做法注意 vec 的大小是严格 \(O(1)\) 的,所以复杂度的 \(O(n\log n)\) 的瓶颈在那个平衡树上。

神经的写法,害得我笑了出来。

http://www.rkmt.cn/news/1943.html

相关文章:

  • 第01周 预习、实验与作业:绪论与Java基本语法
  • 删除字符串中的所有相邻重复项
  • Iframe 全屏嵌入实验
  • VMWare Esxi防火墙添加白名单访问及ip异常无法登录解决办法
  • dw
  • nano快捷键指南
  • 网络通信中的死锁
  • CSP-S模拟19
  • union类型
  • 学习笔记
  • 01_TCP协议概念
  • 【A】chipi chipi chapa chapa
  • linux安装python
  • 【IEEE、电力学科品牌会议】第五届智能电力与系统国际学术会议(ICIPS 2025)
  • CE第9关X64版本问题记录
  • 多态
  • 数学分析 I note
  • 记录一下由于VS中qt的插件自动升级引发的编译问题
  • ck随笔
  • 终结“网络无助感”:Tenable CEO解析漏洞管理与安全心态
  • 生产搭建Hadoop
  • 生产搭建Rabbitmq
  • macOS Tahoe 26 RC (25A353) Boot ISO 原版可引导镜像下载
  • 企业如何选型低代码平台?4款产品测评
  • torch版本应该跟cuda、cudacnn的版本一致
  • 安装mysql数据库,从下载到配置的详细教程
  • [BJOI2018] 染色 题解
  • 金蝶云星空学习记录1
  • (简记)虚树
  • AI测试平台自动遍历:低代码也能玩转全链路测试