bzoj4520 K远点对
思路
这个"\(K\)远“点对一直理解成了距离第\(K\)大的点对\(233\)。
要求第\(K\)远,那么我们只要想办法求出来最远的\(K\)个点对就可以了。
用一个大小为\(2K\)(因为每个点对会被统计两次)的小头堆维护距离最大的\(K\)个点对,然后在\(KD-tree\)上查询最远点对,如果查到的点对之间的距离比堆顶大,那么就把堆顶弹出来,当前距离插进去。
最后堆顶元素就是答案。
代码
/*
* @Author: wxyww
* @Date: 2019-06-13 07:45:59
* @Last Modified time: 2019-06-13 08:47:21
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
#define int ll
const int N = 100100,INF = 1e9;
#define ls TR[rt].ch[0]
#define rs TR[rt].ch[1]
priority_queue<int,vector<int>,greater<int> >q;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int mul(int x) {
return x * x;
}
struct node {
int ch[2],d[2],mx[2],mn[2];
}TMP,TR[N],a[N];
int D;
bool cmp(const node &A,const node &B) {
return A.d[D] < B.d[D];
}
void up(int rt) {
for(int i = 0;i <= 1;++i) {
if(ls) TR[rt].mx[i] = max(TR[ls].mx[i],TR[rt].mx[i]),
TR[rt].mn[i] = min(TR[ls].mn[i],TR[rt].mn[i]);
if(rs) TR[rt].mx[i] = max(TR[rs].mx[i],TR[rt].mx[i]),
TR[rt].mn[i] = min(TR[rs].mn[i],TR[rt].mn[i]);
}
}
int build(int l,int r,int now) {
D = now;
int mid = (l + r) >> 1;
nth_element(a + l,a + mid,a + r + 1,cmp);
TR[mid] = a[mid];
for(int i = 0;i <= 1;++i) TR[mid].mx[i] = TR[mid].mn[i] = TR[mid].d[i];
int rt = mid;
if(l < mid) ls = build(l,mid - 1,now ^ 1);
if(r > mid) rs = build(mid + 1,r,now ^ 1);
up(mid);
return mid;
}
int dis(const node &A,const node &B) {
return mul(A.d[0] - B.d[0]) + mul(A.d[1] - B.d[1]);
}
int get(const node &A,const node &B) {
int ret = 0;
for(int i = 0;i <= 1;++i)
ret += mul(max(abs(A.d[i] - B.mx[i]),abs(A.d[i] - B.mn[i])));
return ret;
}
void query(int rt) {
int K = dis(TMP,TR[rt]);
if(K > q.top()) q.pop(),q.push(K);
int dl = -INF,dr = -INF;
if(ls) dl = get(TMP,TR[ls]);
if(rs) dr = get(TMP,TR[rs]);
if(dl > dr) {
if(dl > q.top()) query(ls);
if(dr > q.top()) query(rs);
}
if(dr > dl) {
if(dr > q.top()) query(rs);
if(dl > q.top()) query(ls);
}
}
int X[N],Y[N];
signed main() {
int n = read(),K = read();
for(int i = 1;i <= n;++i) {
X[i] = a[i].d[0] = read();Y[i] = a[i].d[1] = read();
}
int root = build(1,n,0);
for(int i = 1;i <= K * 2;++i) q.push(0);
for(int i = 1;i <= n;++i) {
TMP.d[0] = X[i],TMP.d[1] = Y[i];
query(root);
}
cout<<q.top();
return 0;
}
bzoj4520 K远点对的更多相关文章
- 【bzoj4520】 Cqoi2016—K远点对
http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...
- 【bzoj4520】K远点对
Portal --> bzoj4520 Description 给你平面内\(n\)个点的坐标,求欧氏距离下第\(k\)远的点对 Solution 因为kd其实..严格来说挺不熟的用的太少了qw ...
- 【BZOJ4520】K远点对(KD-Tree)
[BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...
- 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- [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 ...
- BZOJ4520:[CQOI2016]K远点对(K-D Tree)
Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...
- [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1285 Solved: 708[Submit][Statu ...
随机推荐
- Sphinx 2.0.8 发布,全文搜索引擎 Installing Sphinx on Windows
参考资料地址信息 http://sphinxsearch.com/docs/latest/installing-windows.html http://my.oschina.net/melonol/b ...
- python-openCV 绘制图形
文档链接:https://docs.opencv.org/trunk/dc/da5/tutorial_py_drawing_functions.html 文档描述了OpenCV的几个绘图功能: 绘制圆 ...
- 细数使用View UI(iView)开发中遇到的坑
一.前言 View UI,即原先的 iView,是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产品. 官网地址:https://www.iviewui.com/docs ...
- 分词 | 双向匹配中文分词算法python实现
本次实验内容是基于词典的双向匹配算法的中文分词算法的实现.使用正向和反向最大匹配算法对给定句子进行分词,对得到的结果进行比较,从而决定正确的分词方法. 算法描述正向最大匹配算法先设定扫描的窗口大小ma ...
- Mysql - 存储过程 - 定时删表
在工业监控里面, 需要对每天的数据, 进行记录, 时间长了之后, 数据库很容易撑爆. 这时候, 如果允许, 可以对之前的数据进行一次清除, 只记录几个月内的数据. delimiter $ DROP P ...
- 使用Vuejs 开发chrome 插件的注意事项
chrome 插件的开发其实并不难,web开发者可以使用 html, css, javascript 轻松的开发实用的 chrome 插件. 一个好的 chrome 插件可以提高我们的开发效率,甚至方 ...
- MySQL中的group_concat函数的使用
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- 在Vue中使用i18n 国际化遇到 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
最近用Vue在搭建前端框架,在引用i18n时,运行的时候报错:Uncaught TypeError: Cannot assign to read only property 'exports' of ...
- Python 使用 PyMysql、DBUtils 创建连接池,提升性能
转自:https://blog.csdn.net/weixin_41287692/article/details/83413775 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如 ...
- 用Python制作只属于你和ta的聊天渠道吧
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python应用宝典 PS:如有需要Python学习资料的小伙伴可 ...