4520: [Cqoi2016]K远点对

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 638  Solved: 340
[Submit][Status][Discuss]

Description

已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。

 

Input

输入文件第一行为用空格隔开的两个整数 N, K。接下来 N 行,每行两个整数 X,Y,表示一个点
的坐标。1 < =  N < =  100000, 1 < =  K < =  100, K < =  N*(N−1)/2 , 0 < =  X, Y < 2^31。
 

Output

输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)。

 

Sample Input

10 5
0 0
0 1
1 0
1 1
2 0
2 1
1 2
0 2
3 0
3 1

Sample Output

9

HINT

 

Source

 

[Submit][Status][Discuss]

建立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远点对的更多相关文章

  1. BZOJ 4520 [Cqoi2016]K远点对(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...

  2. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  3. BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 200000 #define inf 10000000000 ...

  4. 【52.55%】【BZOJ 4520】K远点对

    Time Limit: 30 Sec  Memory Limit: 512 MB Submit: 588  Solved: 309 [Submit][Status][Discuss] Descript ...

  5. [Cqoi2016]K远点对 K-Dtree

    4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...

  6. [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1285  Solved: 708[Submit][Statu ...

  7. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

    [BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...

  8. 【bzoj4520】 Cqoi2016—K远点对

    http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...

  9. [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 ...

随机推荐

  1. 基于Centos7的比特币源码编译

    因为一直比较熟悉Windows平台的开发,本来打算在windows下进行编译,但查了一些文章,发现还是在linux下编译成本最低,所以这里就以手头现有的Centos7环境进行代码编译.   一.代码获 ...

  2. List集合中的对象进行排序

    类A: public class A implements Comparable<A>{ private Integer id; private String name; public A ...

  3. 服务器与Linux操作系统基础原理

    1.服务器 2.Linux操作系统 1. 服务器 服务器定义与分类: 定义:一个管理资源并为用户提供服务的计算机软件. 按应用分类:通常分为文件服务器(能使用户在其它计算机访问文件),数据库服务器和应 ...

  4. Windows 本地文件搜索神器

    Wox: Windows 本地文件搜索神器 下载地址: https://github.com/Wox-launcher/Wox 注: Wox只能搜索C盘下的文件,所以需要结合everything 如果 ...

  5. openssl在多平台和多语言之间进行RSA加解密注意事项

    首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...

  6. html , body , margin , overflow 之大乱战

    <!DOCTYPE html> <html> <head> <style> html,body{ margin:0 ;padding:0 } div{m ...

  7. 关于手机端h5上传图片配合exif.min.js,processImg.js的使用

    首先这里有个new FileReader()的概念,这是h5新增的,用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件 ...

  8. sprint2(第九天)

    今天是sprint2的最后一天,已经完成功能有可以实现点餐功能.菜品的添加和删减.菜品数量的增减.添加备注.查看订单详情.订单状态.提交订单.后厨可以查看订单信息,对菜品的状态进行操作,是否完成烹饪, ...

  9. LeetCode 655. Print Binary Tree (C++)

    题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...

  10. Hive问题

    今天一直遇到一个问题: 在查询最热10个关键词时候总是报错,下图为报错最下面 一直关注着failed的内容,头疼了一天.........  结果实验室老哥给指出问题,是yarnException报错, ...