【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 ...
随机推荐
- LeetCode OJ-- Insertion Sort List **
https://oj.leetcode.com/problems/insertion-sort-list/ 链表实现插入排序 首先插入排序是指: a b c d e g m 对b也就是第二个位置选做元 ...
- 查询/删除/建立DB2数据表的主键
一.查询表主键. describe indexes for table <instancename>.<tablename> 例: describe indexes for t ...
- Bootstrap多层模态框modal嵌套问题
一.问题 在项目里忽然新加了一个需求,在原本弹出的模态框里,点击模态框里面的按钮再弹出一个模态框,出来另个模态框来展示详细信息.此时就存在两个模态框在这个需求没加之前有一个弹出的模态框也是需要继续点击 ...
- BZOJ2870—最长道路tree
最长道路tree Description H城很大,有N个路口(从1到N编号),路口之间有N-1边,使得任意两个路口都能互相到达,这些道路的长度我们视作一样.每个路口都有很多车辆来往,所以每个路口i都 ...
- 洛谷——1115 最大子段和(区间DP)
题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N个绝对值不大于10000 ...
- bzoj 2889: Tree Conundrum
2889: Tree Conundrum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 37[Submit][Status][ ...
- JavaScript 深克隆
深克隆 function judgeType(arg){//判断js数据类型 return Object.prototype.toString.call(arg).slice(8,-1); } fun ...
- PHP计算两个时间的年数、月数以及天数
如何获取两个不同时间相差几年几月几日呢?比如当前时间距离2008年08月08日的北京奥运会有几年几月几日了?需要说明的是:1.定义一年为360天,一个月为30天:2.代码中86400=24*60*60 ...
- 2016.7.14 generator基于注解和基于xml自动生成代码的区别
1.generator配置文件generatorConfig.xml的区别 2.生成代码的区别 注:二者的实体类都一样. (1)基于XML 生成的文件有: 后面省略. 也就是说,基于xml的方式,是要 ...
- eletron 播放rtmp flash 播放器问题
1 安装 flash https://www.flash.cn/ 2 man.js 配置 参考 https://newsn.net/say/electron-flash-win.html 3 播放器 ...