题目链接

思路

这个"\(K\)远“点对一直理解成了距离第\(K\)大的点对\(233\)。

要求第\(K\)远,那么我们只要想办法求出来最远的\(K\)个点对就可以了。

用一个大小为\(2K\)(因为每个点对会被统计两次)的小头堆维护距离最大的\(K\)个点对,然后在\(KD-tree\)上查询最远点对,如果查到的点对之间的距离比堆顶大,那么就把堆顶弹出来,当前距离插进去。

最后堆顶元素就是答案。

代码

/*
* @Author: wxyww
* @Date: 2019-06-13 07:45:59
* @Last Modified time: 2019-06-13 08:47:21
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
#define int ll
const int N = 100100,INF = 1e9;
#define ls TR[rt].ch[0]
#define rs TR[rt].ch[1]
priority_queue<int,vector<int>,greater<int> >q;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int mul(int x) {
return x * x;
}
struct node {
int ch[2],d[2],mx[2],mn[2];
}TMP,TR[N],a[N];
int D;
bool cmp(const node &A,const node &B) {
return A.d[D] < B.d[D];
}
void up(int rt) {
for(int i = 0;i <= 1;++i) {
if(ls) TR[rt].mx[i] = max(TR[ls].mx[i],TR[rt].mx[i]),
TR[rt].mn[i] = min(TR[ls].mn[i],TR[rt].mn[i]);
if(rs) TR[rt].mx[i] = max(TR[rs].mx[i],TR[rt].mx[i]),
TR[rt].mn[i] = min(TR[rs].mn[i],TR[rt].mn[i]);
}
}
int build(int l,int r,int now) {
D = now;
int mid = (l + r) >> 1;
nth_element(a + l,a + mid,a + r + 1,cmp);
TR[mid] = a[mid];
for(int i = 0;i <= 1;++i) TR[mid].mx[i] = TR[mid].mn[i] = TR[mid].d[i];
int rt = mid;
if(l < mid) ls = build(l,mid - 1,now ^ 1);
if(r > mid) rs = build(mid + 1,r,now ^ 1);
up(mid);
return mid;
}
int dis(const node &A,const node &B) {
return mul(A.d[0] - B.d[0]) + mul(A.d[1] - B.d[1]);
}
int get(const node &A,const node &B) {
int ret = 0;
for(int i = 0;i <= 1;++i)
ret += mul(max(abs(A.d[i] - B.mx[i]),abs(A.d[i] - B.mn[i])));
return ret;
}
void query(int rt) {
int K = dis(TMP,TR[rt]);
if(K > q.top()) q.pop(),q.push(K);
int dl = -INF,dr = -INF;
if(ls) dl = get(TMP,TR[ls]);
if(rs) dr = get(TMP,TR[rs]);
if(dl > dr) {
if(dl > q.top()) query(ls);
if(dr > q.top()) query(rs);
}
if(dr > dl) {
if(dr > q.top()) query(rs);
if(dl > q.top()) query(ls);
}
}
int X[N],Y[N];
signed main() {
int n = read(),K = read();
for(int i = 1;i <= n;++i) {
X[i] = a[i].d[0] = read();Y[i] = a[i].d[1] = read();
}
int root = build(1,n,0);
for(int i = 1;i <= K * 2;++i) q.push(0);
for(int i = 1;i <= n;++i) {
TMP.d[0] = X[i],TMP.d[1] = Y[i];
query(root);
}
cout<<q.top();
return 0;
}

bzoj4520 K远点对的更多相关文章

  1. 【bzoj4520】 Cqoi2016—K远点对

    http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...

  2. 【bzoj4520】K远点对

    Portal --> bzoj4520 Description 给你平面内\(n\)个点的坐标,求欧氏距离下第\(k\)远的点对 Solution 因为kd其实..严格来说挺不熟的用的太少了qw ...

  3. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...

  4. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

    [BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...

  5. [bzoj4520][Cqoi2016]K远点对_KD-Tree_堆

    K远点对 bzoj-4520 Cqoi-2016 题目大意:已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. 注释:$1\le n\le 10^5$,$1\le k\le 100$,$k\l ...

  6. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  7. 【BZOJ-4520】K远点对 KD-Tree + 堆

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 490  Solved: 237[Submit][Status ...

  8. BZOJ4520:[CQOI2016]K远点对(K-D Tree)

    Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...

  9. [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1285  Solved: 708[Submit][Statu ...

随机推荐

  1. vscode源码分析【八】加载第一个画面

    第一篇: vscode源码分析[一]从源码运行vscode 第二篇:vscode源码分析[二]程序的启动逻辑,第一个窗口是如何创建的 第三篇:vscode源码分析[三]程序的启动逻辑,性能问题的追踪 ...

  2. Saiku ui-settings接口404错误避免(二十九)

    Saiku ui-settings接口404错误避免 自己手动编译的saiku ,不知道为什么前端总是报错   /saiku/rest/saiku/info/ui-settings  404NotFo ...

  3. 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 4

    23.2.3  在程序中访问接口 本节再优化userapi.php文件中的代码,并多加一个接口函数store(), 模拟一个表单,通过POST提交数据给它,验证并将数据添加到数据库中,代码如下所示: ...

  4. ARM64的内核栈、用户栈、寄存器上下文

    1. 内核栈的分配,即thread_info的分配,是在do_fork->dup_task_struct中分配(默认为2个pages),并赋值给task_struct->stack: 2. ...

  5. keras EfficientNet介绍,在ImageNet任务上涨点明显 | keras efficientnet introduction

    本文首发于个人博客https://kezunlin.me/post/88fbc049/,欢迎阅读最新内容! keras efficientnet introduction Guide About Ef ...

  6. OpenGL光照3:光源

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...

  7. 帝国cms提高网站网页打开速度的手段

    1.减少页面HTTP请求数量 2.使用CDN(Content Delivery Network)网络加速 3.添加文件过期或缓存头 4.服务器开启gzip压缩 5.css格式定义放置在文件头部 6.J ...

  8. python xlwt模块简介

    一.基础类介绍 1.工作簿类Workbook简介: import xlwt class Workbook(object0): ''' 工作簿类,使用xlwt创建excel文件时,首先要实例化此类的对象 ...

  9. DDL、DML、TCL

    一.DDL 1.创建表(CREATE) (1)数据库对大小写不敏感,只对字符串大小写敏感. (2)使用create关键字创建表.(-- 表示注释). 格式: CREATE TABLE 表名( 字段名1 ...

  10. js 时分秒转化为秒

    var time = '00:02:10'; var hour = time.split(':')[0]; var min = time.split(':')[1]; var sec = time.s ...