【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
【BZOJ4520】[Cqoi2016]K远点对
Description
已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。
Input
Output
输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)。
Sample Input
0 0
0 1
1 0
1 1
2 0
2 1
1 2
0 2
3 0
3 1
Sample Output
题解:我们枚举每个点在kd-tree上查询,同时维护一个小根堆,一旦某个点离当前点的距离>堆顶,就将它加入堆并弹出堆顶,最后的堆顶就是答案。
由于每个点对都被算了两边,所以k应该*2。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define rep for(int i=0;i<=1;i++)
#define x2(_) ((_)*(_))
using namespace std;
const int maxn=100010;
typedef long long ll;
struct kd
{
ll v[2],sm[2],sn[2];
int ls,rs;
ll & operator [] (int a) {return v[a];}
kd (){}
kd (ll a,ll b){v[0]=sm[0]=sn[0]=a,v[1]=sm[1]=sn[1]=b,ls=rs=0;}
}t[maxn];
priority_queue<ll> pq;
int n,m,D,root,now;
ll A,B;
bool cmp(kd a,kd b)
{
return (a[D]==b[D])?(a[D^1]<b[D^1]):(a[D]<b[D]);
}
void pushup(int x,int y)
{
rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);
}
int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=l+r>>1;
D=d,nth_element(t+l,t+mid,t+r+1,cmp);
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
ll getdis(int x)
{
return max(x2(t[x].sm[0]-A),x2(t[x].sn[0]-A))+max(x2(t[x].sm[1]-B),x2(t[x].sn[1]-B));
}
void query(int x)
{
if(!x||getdis(x)<=-pq.top()) return ;
if(x!=now&&x2(t[x][0]-A)+x2(t[x][1]-B)>-pq.top()) pq.push(-x2(t[x][0]-A)-x2(t[x][1]-B)),pq.pop();
if(getdis(t[x].ls)>getdis(t[x].rs)) query(t[x].ls),query(t[x].rs);
else query(t[x].rs),query(t[x].ls);
}
int main()
{
n=rd(),m=rd();
int i,a,b;
for(i=1;i<=n;i++) a=rd(),b=rd(),t[i]=kd(a,b);
for(i=1;i<=2*m;i++) pq.push(0);
root=build(1,n,0);
for(i=1;i<=n;i++) now=i,A=t[i].v[0],B=t[i].v[1],query(root);
printf("%lld\n",-pq.top());
return 0;
}
【BZOJ4520】[Cqoi2016]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][Cqoi2016]K远点对 kd-tree 优先队列
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1285 Solved: 708[Submit][Statu ...
- [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 ...
- BZOJ4520 [Cqoi2016]K远点对
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- BZOJ 4520: [Cqoi2016]K远点对
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 638 Solved: 340[Submit][Status ...
- 【BZOJ4520】K远点对(KD-Tree)
[BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...
- [Cqoi2016]K远点对 K-Dtree
4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...
随机推荐
- interview ms1 N_Dorm
判断是否为n回文,比如 a b a 是1 回文, abcdab是2回文. 输入: abcabc|3 这种格式,输出true or false #include <iostream> #in ...
- 【转载】ASP.NET 生成验证码
直接上code using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- Java IO 学习(三)缓冲IO / 直接IO / 内存映射
缓冲IO 在介绍缓冲IO之前需要先了解一下常用的机械硬盘的原理与特点 一个机械硬盘中装有多个盘片 每个盘片上有多个同心圆(磁道) 每个同心圆又由多个弧(扇区)组成,每个弧上都记录了等量的数据(比方说5 ...
- Apache + mod_wsgi部署webpy应用
Apache + mod_wsgi部署webpy应用 引用:http://webpy.org/cookbook/mod_wsgi-apache.zh-cn 下面的步骤在Apache-2.2.3 ( ...
- SpringBoot整合MyBatisPlus配置动态数据源
目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...
- black hack
黑客技 关于在不知道系统的情况下 long long 的使用时 那么 #ifdef WIN32 #define LL "%I64d" #else #define LL " ...
- CodeForces - 258D Little Elephant and Broken Sorting
Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...
- 查看tomcat启动文件都干点啥---Bootstrap.java
在上一章查看tomcat启动文件都干点啥---catalina.bat,说了在catalina.bat中都走了什么流程,最重要的是,我们得出了如下这段命令: _EXECJAVA=start " ...
- Jenkins配置git进行构建失败:Error cloning remote repo 'origin'的解决思路
说明:这个没有实际的解决方法,只提供一个思路去解决. 操作系统:windows 背景:在配置的节点之后,由于是windows的系统,运行git克隆地址,使用的是SSH协议地址.出现如下的错误: Err ...
- xshell登录到CentOS7上时出现“The remote SSH server rejected X11 forwarding request.
其原因是肯能对openssh版本进行了升级. 解决方法为: yum install xorg-x11-font* xorg-x11-xauth /etc/ssh/sshd ...