BZOJ 4520: [Cqoi2016]K远点对
4520: [Cqoi2016]K远点对
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 638 Solved: 340
[Submit][Status][Discuss]
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
HINT
Source
建立KD-Tree,用堆维护当前找到的前K远点对。
枚举一个点,在树中遍历,尝试更新堆顶(较近的点对)。
注意用KD-Tree“估价函数”来剪枝,还有因为点对有重复,所以应当取出第2K远的点对。
#include <bits/stdc++.h> const int siz = ; int n, m; struct node
{
int son[];
int pos[];
int maxi[];
int mini[];
}tr[siz]; int cmpk; inline bool cmp(const node &a, const node &b)
{
if (a.pos[cmpk] != b.pos[cmpk])
return a.pos[cmpk] < b.pos[cmpk];
else
return a.pos[cmpk^] < b.pos[cmpk^];
} int build(int l, int r, int k)
{
int mid = (l + r) >> ; cmpk = k; std::nth_element(tr + l, tr + mid, tr + r + , cmp); if (l < mid)tr[mid].son[] = build(l, mid - , k ^ );
if (r > mid)tr[mid].son[] = build(mid + , r, k ^ ); tr[mid].maxi[] = tr[mid].mini[] = tr[mid].pos[];
tr[mid].maxi[] = tr[mid].mini[] = tr[mid].pos[]; for (int i = ; i < ; ++i)if (tr[mid].son[i])
for (int j = ; j < ; ++j)
{
if (tr[mid].maxi[j] < tr[tr[mid].son[i]].maxi[j])
tr[mid].maxi[j] = tr[tr[mid].son[i]].maxi[j];
if (tr[mid].mini[j] > tr[tr[mid].son[i]].mini[j])
tr[mid].mini[j] = tr[tr[mid].son[i]].mini[j];
} return mid;
} typedef long long lnt; const lnt inf = 2e18; std::priority_queue<
lnt, std::vector<lnt>, std::greater<lnt>
> heap; int qx, qy; inline lnt sqr(lnt x)
{
return x * x;
} inline lnt calc(int t)
{
lnt dx = std::max(sqr(tr[t].mini[] - qx), sqr(tr[t].maxi[] - qx));
lnt dy = std::max(sqr(tr[t].mini[] - qy), sqr(tr[t].maxi[] - qy)); return dx + dy;
} void query(int t)
{
lnt dis = sqr(tr[t].pos[] - qx) + sqr(tr[t].pos[] - qy); if (dis > heap.top())
{
heap.pop();
heap.push(dis);
} lnt dl = tr[t].son[] ? calc(tr[t].son[]) : -inf;
lnt dr = tr[t].son[] ? calc(tr[t].son[]) : -inf; if (dl > dr)
{
if (dl > heap.top())query(tr[t].son[]);
if (dr > heap.top())query(tr[t].son[]);
}
else
{
if (dr > heap.top())query(tr[t].son[]);
if (dl > heap.top())query(tr[t].son[]);
}
} signed main(void)
{
scanf("%d%d", &n, &m); for (int i = ; i <= n; ++i)
scanf("%d%d", &tr[i].pos[], &tr[i].pos[]); int root = build(, n, ); for (int i = ; i <= *m; ++i)
heap.push(0LL); for (int i = ; i <= n; ++i)
{
qx = tr[i].pos[];
qy = tr[i].pos[];
query(root);
} printf("%lld\n", heap.top());
}
@Author: YouSiki
BZOJ 4520: [Cqoi2016]K远点对的更多相关文章
- BZOJ 4520 [Cqoi2016]K远点对(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...
- BZOJ 4520: [Cqoi2016]K远点对(k-d tree)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1162 Solved: 618[Submit][Status][Discuss] Descripti ...
- BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆
Code: #include<bits/stdc++.h> #define ll long long #define maxn 200000 #define inf 10000000000 ...
- 【52.55%】【BZOJ 4520】K远点对
Time Limit: 30 Sec Memory Limit: 512 MB Submit: 588 Solved: 309 [Submit][Status][Discuss] Descript ...
- [Cqoi2016]K远点对 K-Dtree
4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...
- [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+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- 【bzoj4520】 Cqoi2016—K远点对
http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...
- [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 ...
随机推荐
- Python字符串符号:双引号/单引号用法注解。
众所周知python中单引号和双引号常常被我们所使用,例如print.input等等. 但是对于打印输出所引导的字符串大多都是用双引号的形式来做,"Hello,python!",而 ...
- WebGL——水波纹特效
大家好,今天我ccentry要做一个水波纹特效,我们来看看水波纹特效的做法.首先我们来看一下水波纹特效的效果是怎么样的,请看下图. 我们要做的就是类似这种纹理特效,那么我们来看看是如何制作的吧.首先鲫 ...
- 【SIKIA计划】_06_Unity2D游戏开发-拾荒者笔记
[新增分类]Animations 动画——Animation——AnimatorControllerPrefabs 预制 [素材导入]unitypackage 素材包Sprites Editor 编辑 ...
- Unity_屏幕/Viewport/世界坐标的转换
Unity_屏幕/Viewport/世界/UI坐标的转换 参考: https://www.jianshu.com/p/b5b6ac9ab145 -- 世界.视口.屏幕坐标转换 https://docs ...
- keyup在移动端失效解决方法
keyup在移动端失效解决方法: $("#OBJ").on("input propertychange", function(){ }); 采用 input 与 ...
- GIT问题(一)——push冲突
- js中 null, undefined, 0,空字符串,false,不全等比较
null == undefined // true null == '' // false null == 0 // false null == false // false undefined = ...
- python操作hive并且获取查询结果scheam
执行hive -e 命令并且获取对应的select查询出来的值及其对应的scheam字段 需要在执行语句中前部添加 set hive.cli.print.header=true; 这个设置,如下语句: ...
- iOS静态库.a总结(2017.1.24增加脚本打包方法)
修改于:2017.1.24 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.根据源代码的公开情况,库可以分为2种类型 a.开源库 公开源代码,能看到具体实现 ,比如SDWebImag ...
- C++ 类 构造函数 constructor
构造函数 当定义了一个整型变量: int a; 这会申请了一块内存空间来存储a,但是这块内存中原本有数据的,可能是任何值,这不是你所希望的,若你就希望a表示1,所以要把a的值赋值为1. ; 例: #i ...