本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

Description

已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。

 

Input

输入文件第一行为用空格隔开的两个整数 N, K。接下来 N 行,每行两个整数 X,Y,表示一个点
的坐标。1 < =  N < =  100000, 1 < =  K < =  100, K < =  N*(N−1)/2 , 0 < =  X, Y < 2^31。
 

Output

输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)。

 

Sample Input

10 5
0 0
0 1
1 0
1 1
2 0
2 1
1 2
0 2
3 0
3 1

Sample Output

9
 
 

正解:kd-tree

解题报告:

  这题要求欧氏距离下的第k远点对。

  我们考虑上$kd-tree$。$kd-tree$可以支持我们对于某一个给定的点与$kd-tree$中的点的距离最值查询,那么我们很容易发现,我们只需要对于每个点都分别与其他所有点求距离,最后对于全局取第$2*k$大的距离即可(因为每个距离我们算了两次)。

  但是直接暴力的话会变成$ n^2 $,如果改在$kd-tree$上查询的话就可以大大节约时间,可以减掉大量无用状态。具体做法就是,维护一个大小为$2*k$的小根堆,对于每个点在$kd-tree$上查询与其他点的距离,如果大于堆顶,则删掉堆顶元素,把当前距离加入堆顶。最后只需输出堆顶元素即可。

  kd-tree在这里就是优化暴力......

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int MAXN = 100011;
int n,k,nowD,root,ql,qr;
struct node{ LL dis; inline bool operator < (const node &a) const { return a.dis<dis; } }tmp;
struct KDTree{ int d[2],Min[2],Max[2],l,r; }t[MAXN];
inline bool cmp(KDTree q,KDTree qq){ if(q.d[nowD]==qq.d[nowD]) return q.d[nowD^1]<qq.d[nowD^1]; return q.d[nowD]<qq.d[nowD]; }
inline void MAX(KDTree &q,KDTree qq,int type){ if(qq.Max[type]>q.Max[type]) q.Max[type]=qq.Max[type]; }
inline void MIN(KDTree &q,KDTree qq,int type){ if(qq.Min[type]<q.Min[type]) q.Min[type]=qq.Min[type]; }
inline LL sqr(LL x){ return x*x; }
inline LL getdis(int x,int y){ return sqr(t[x].d[0]-t[y].d[0])+sqr(t[x].d[1]-t[y].d[1]); }
inline LL almost_dis(KDTree q,KDTree qq){return max(sqr(q.d[0]-qq.Min[0]),sqr(q.d[0]-qq.Max[0]))+max(sqr(q.d[1]-qq.Min[1]),sqr(q.d[1]-qq.Max[1]));}
priority_queue<node>Q;
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void update(int x){
if(t[x].l) for(int i=0;i<2;i++) MAX(t[x],t[t[x].l],i),MIN(t[x],t[t[x].l],i);
if(t[x].r) for(int i=0;i<2;i++) MAX(t[x],t[t[x].r],i),MIN(t[x],t[t[x].r],i);
} inline int build(int l,int r,int D){
nowD=D; int mid=(l+r)>>1; nth_element(t+l+1,t+mid+1,t+r+1,cmp);
if(l<mid) t[mid].l=build(l,mid-1,D^1);
if(mid<r) t[mid].r=build(mid+1,r,D^1);
t[mid].Min[0]=t[mid].Max[0]=t[mid].d[0];
t[mid].Min[1]=t[mid].Max[1]=t[mid].d[1];
update(mid);
return mid;
} inline void query(int u){
LL dl=0,dr=0,dd=getdis(0,u); if(dd>Q.top().dis) Q.pop(),tmp.dis=dd,Q.push(tmp);
if(t[u].l) dl=almost_dis(t[0],t[t[u].l]); if(t[u].r) dr=almost_dis(t[0],t[t[u].r]);
tmp=Q.top();
if(dl>dr) {
if(dl>tmp.dis) query(t[u].l); tmp=Q.top();
if(dr>tmp.dis) query(t[u].r);
}
else{
if(dr>tmp.dis) query(t[u].r); tmp=Q.top();
if(dl>tmp.dis) query(t[u].l);
}
} inline void work(){
n=getint(); k=getint(); for(int i=1;i<=n;i++) t[i].d[0]=getint(),t[i].d[1]=getint();
root=build(1,n,0); tmp.dis=0;
for(int i=1;i<=k*2;i++) Q.push(tmp);
for(int i=1;i<=n;i++) {
t[0].d[0]=t[i].d[0]; t[0].d[1]=t[i].d[1];
query(root);
}
printf("%lld",Q.top().dis);
} int main()
{
work();
return 0;
}

  

BZOJ4520 [Cqoi2016]K远点对的更多相关文章

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

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

  2. [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 ...

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

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

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

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

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

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

  6. BZOJ 4520: [Cqoi2016]K远点对

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

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

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

  8. [Cqoi2016]K远点对 K-Dtree

    4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...

  9. 【bzoj4520】 Cqoi2016—K远点对

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

随机推荐

  1. C++算法实源码分析

    includes: // TEMPLATE FUNCTION includes WITH PRED template<class _InIt1, class _InIt2, class _Pr& ...

  2. ORACLE数据库汉字占几个字节问题

    一同事由于系统需求关系,将SQL SERVER数据库的一个表导入ORACLE数据库时,发现居然报错:ORA-12899: value too large for column xxxx (actual ...

  3. expdp 报The value (30) of MAXTRANS parameter ignored错误的原因诊断

    在使用expdp导出一个表的数据时遇到了下面情况,也不见其提示报错信息,一下子就执行完了,也没有导出我需要的数据 [oracle@getlnx01 dump_dir]$ expdp system/xx ...

  4. DPA/Ignite由于DNS问题导致连接不上被监控的数据库服务器

    问题描述: 在DPA(Ignite)的管理监控界面发现有两台SQL Server数据库服务器连接不上,截图如下所示,检查其日志内容 具体错误日志如下所示, Notice:日志里面具体的服务器名称被我用 ...

  5. hive建表与数据的导入导出

    建表: create EXTERNAL table tabtext(IMSI string,MDN string,MEID string,NAI string,DestinationIP string ...

  6. Elasticsearch配置文件说明

    一.Cluster  setting Cluster indices.ttl.interval  允许设置多久过期的文件会被自动删除.默认值是60秒. indices.cache.filter.siz ...

  7. Python安装

    Win10 安装Python 1.官网下载Python安装包,一直点击下一步进行安装 安装完Python后要设置环境变量,右键我的电脑-->属性-->高级系统设置-->环境变量--& ...

  8. x01.os.20: compile linux-0.11 on the ubuntu

    为什么学习 linux 正如不能依靠美国的 GPS 为我们的导弹指示目标一样,很难想像用运行 windows 的电脑去同美国进行信息战.而朝鲜的网络崩溃,再次警示国人,信息战.网络战离我们并不遥远.l ...

  9. Linux初学:(二)Shell环境与命令基础

    博客园(FOREVER_ENJOY):http://www.cnblogs.com/zyx1314/ 本文版权归作者所有:欢迎转载!请注明文章作者和原文连接 Shell是什么? 1. Shell作为应 ...

  10. jQuery表单验证案例

    目标:使用jQuery进行表单验证. 功能:1.必填选项后面添加了红色小星星: 2.选中开始输入时,输入文本框会改变当前背景色,增强用户体验: 3.输入的时候就开始验证,当输入格式正确就会提醒,就是当 ...