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. Oracle之带参存储过程(存储过程中for循环调用存储过程)

    --带参存储过程create or replace procedure testdate(v in number) is i number; begin i:=v; insert into test_ ...

  2. Hyperledger Fabric 1.0 从零开始(一)

    在HyperLedger/Fabric发布0.6的时候,公司就已经安排了一个团队研究这一块,后来也请IBM的专家组过来培训了一批人,不幸的是,这批人后来全走了,然后1.0就发布了.自从2017年7月H ...

  3. Python常用模块之PIL(手册篇:Image模块)

    官方手册地址:http://effbot.org/imagingbook/image.htm  Image模块 图像模块提供了一个具有相同名称的类,用于表示一个PIL的图像.该模块还提供了许多功能,包 ...

  4. 作业1MathExam

    自己取一个大气又可爱的标题 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 720 1000 ...

  5. 【数据预处理】TIMIT语料库WAV文件转换

    1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...

  6. binding(转)

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  7. mysql非安装包安装教程

    设置mysql的环境变量 本人设置安装的路径是:E:\WebApplication\webMySQL\mysql-5.7.13-winx64 我的电脑 ---> 高级系统配置 ---> 环 ...

  8. 【动态规划】POJ-2229

    一.题目 Description Farmer John commanded his cows to search for different sets of numbers that sum to ...

  9. OSI协议和TCP/IP协议笔记

    1.OSI协议: 第7层应用层:OSI中的最高层.是用户与网络的接口.该层通过应用程序来完成网络用户的应用需求,如文件传输.收发电子邮件等.在此常见的协议有:HTTP,HTTPS,FTP,TELNET ...

  10. iOS 怎么自定制推送声音呢?(APP运行时和APP进入后台时)

    说明: 一般如果修改了apple官方的推送声音后,则APP进入后台后,推送会播放开发者自定制的推送声音,而用户在使用APP(也就是APP运行时)的时候,一般是不会有推送声音,因为此时的推送内容已经呈现 ...