【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 ...
随机推荐
- windows下apache+php配置 问题总结
以下为转帖内容: 原文出处:http://www.cnblogs.com/angelox/archive/2008/10/09/1306732.html PHP5+APACHE2.2配置成功案例:第一 ...
- Linux日志审计
https://www.itgank.com/archives/2599 https://blog.csdn.net/yanggd1987/article/details/70255179 http: ...
- (1)jquery基本用法
引入jquery 本地引用 <script src="jquery-3.2.1.js"></script> 网络引用 谷歌CDN <script sr ...
- 最短Hamilton路径
题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每 ...
- bzoj 5125: [Lydsy1712月赛]小Q的书架
新学了一波 决策单调性 dp 套路.... 这种dp一般是长这样的 => f[i][j] = max/min { f[i-1][k] + cost(k+1,j)} ,其中cost函数满足四边形 ...
- 从客户端(ExportContent="...ontinuous <br />Pass Count":13...")中检测到有潜在危险的 Request.Form 值。
mvc中,用chrome浏览器导出excel提示如题错误的解决办法. <system.web> <httpRuntime requestValidationMode="2. ...
- novell.directory.ldap获取邮箱活动目录
在windows系统上可以使用下列方法来查找所有的员工邮箱和员工组: StringDictionary ReturnArray = new StringDictionary(); Dictionary ...
- windows核心编程 DLL技术 【转】
注:本文章转载于网络,源地址为:http://blog.csdn.net/ithzhang/article/details/7051558 本篇文章将介绍DLL显式链接的过程和模块基地址重定位及模块绑 ...
- Youtube深度学习推荐系统论文
https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45530.pdf https://zh ...
- Python批量复制和重命名文件
Python批量复制和重命名文件 示例代码 #! /usr/bin/env python # coding=utf-8 import os import shutil import time impo ...