【BZOJ4520】K远点对(KD-Tree)
【BZOJ4520】K远点对(KD-Tree)
题面
题解
考虑暴力。
维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶
这样子可以用\(KD-Tree\)优化:
如果当前平面内可以产生的最远距离小于堆顶,则证明这个平面内的点无法产生贡献
就不需要计算了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 111111
#define sqr(x) (1ll*(x)*(x))
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define ls (t[o].ch[0])
#define rs (t[o].ch[1])
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int D,n,K,rt;
struct Node{ll d[2];}a[MAX];
bool operator<(Node a,Node b){return a.d[D]<b.d[D];}
struct KD_Node{int mn[2],mx[2],ch[2];Node a;}t[MAX];
priority_queue<ll,vector<ll>,greater<ll> >Q;
void update(int x,int y)
{
cmin(t[x].mn[0],t[y].mn[0]);cmin(t[x].mn[1],t[y].mn[1]);
cmax(t[x].mx[0],t[y].mx[0]);cmax(t[x].mx[1],t[y].mx[1]);
}
int Build(int l,int r,int d)
{
D=d;int o=(l+r)>>1;
nth_element(&a[l],&a[o],&a[r+1]);
t[o].mn[0]=t[o].mx[0]=t[o].a.d[0]=a[o].d[0];
t[o].mn[1]=t[o].mx[1]=t[o].a.d[1]=a[o].d[1];
if(l<o)ls=Build(l,o-1,d^1),update(o,ls);
if(o<r)rs=Build(o+1,r,d^1),update(o,rs);
return o;
}
ll Dis(Node a,Node b){return sqr(a.d[0]-b.d[0])+sqr(a.d[1]-b.d[1]);}
ll Dis(Node a,KD_Node b)
{
ll ret=0;
ret=max(ret,Dis(a,(Node){b.mn[0],b.mn[1]}));
ret=max(ret,Dis(a,(Node){b.mn[0],b.mx[1]}));
ret=max(ret,Dis(a,(Node){b.mx[0],b.mn[1]}));
ret=max(ret,Dis(a,(Node){b.mx[0],b.mx[1]}));
return ret;
}
void Query(int o,Node a)
{
ll dis=Dis(a,t[o].a);
if(dis>Q.top())Q.pop(),Q.push(dis);
if(ls){dis=Dis(a,t[ls]);if(dis>Q.top())Query(ls,a);}
if(rs){dis=Dis(a,t[rs]);if(dis>Q.top())Query(rs,a);}
}
int main()
{
n=read();K=read();
for(int i=1;i<=n;++i)a[i].d[0]=read(),a[i].d[1]=read();
rt=Build(1,n,0);
for(int i=1;i<=K+K;++i)Q.push(0);
for(int i=1;i<=n;++i)Query(rt,a[i]);
printf("%lld\n",Q.top());
return 0;
}
【BZOJ4520】K远点对(KD-Tree)的更多相关文章
- BZOJ4520:[CQOI2016]K远点对(K-D Tree)
Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...
- BZOJ 4520: [Cqoi2016]K远点对(k-d tree)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1162 Solved: 618[Submit][Status][Discuss] Descripti ...
- bzoj4520 K远点对
题目链接 思路 这个"\(K\)远"点对一直理解成了距离第\(K\)大的点对\(233\). 要求第\(K\)远,那么我们只要想办法求出来最远的\(K\)个点对就可以了. 用一个大 ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- 【bzoj4520】K远点对
Portal --> bzoj4520 Description 给你平面内\(n\)个点的坐标,求欧氏距离下第\(k\)远的点对 Solution 因为kd其实..严格来说挺不熟的用的太少了qw ...
- BZOJ4520:[CQOI2016]K远点对
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- 【bzoj4520】 Cqoi2016—K远点对
http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...
- [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 ...
随机推荐
- 使用PowerDesign15反向生成数据库
在Pd15中建立物理模型后,可以通过反向工程直接生成数据库的表结构.主要有以下几个步骤: 1. 首先设置一下数据库配置,选择对应要使用的数据库(此处选择Sql Server 2008 R ...
- 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)
题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...
- 「日常训练」 Yukari's Birthday(ZOJ-3665)
题意与分析 二分题.考虑到n的范围是\(10^{12}\),注意到等比公式\(S=a_1\frac{1-q^n}{1-q} (q\ne 1)\),可以看出,不论q有多大(1除外,这个时候\(r=1,k ...
- linux部署MantisBT(三)部署MantisBT
三.部署MantisBT 1.下载MantisBT https://www.mantisbt.org/download.php 2.将MantisBT安装包放在/apache/htdocs下并重命名为 ...
- 使用Iview Menu 导航菜单(非 template/render 模式)
1.首先直接参照官网Demo例子,将代码拷贝进项目中运行, 直接报错: Cannot read property 'mode' of undefined. 然后查看官网介绍,有一行注意文字,好吧. 2 ...
- [JSON].result()
语法:[JSON].result() 返回:[True | False] 说明:用json字符串创建JSON实例时,如果该json字符串不是合法的json格式,会创建一个空的json实例.但是我们如果 ...
- vuex-Actions的用法
Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 是异步的,mutation是同步的. 沿用vuex学习---简介的案例 ...
- 集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096
题目描述: #include<iostream> #include<string> #include<set> #include<map> #inclu ...
- 机器学习-聚类Clustering
简介 前面介绍的线性回归,SVM等模型都是基于数据有标签的监督学习方法,本文介绍的聚类方法是属于无标签的无监督学习方法.其他常见的无监督学习还有密度估计,异常检测等. 聚类就是对大量未知标注的数据集, ...
- Python3 Tkinter-Toplevel
1.创建 Toplevel与Frame类似,但是它包含窗体属性(如Title) from tkinter import * root=Tk() tl=Toplevel() Label(tl,text= ...