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. 关于springcloud的一些问题总结.txt

    @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCo ...

  2. 报错android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual method 'boolean

    出现这种问题,打开Android monitor的调试信息发现是 android.view.InflateException: Binary XML file line #11: Attempt to ...

  3. Table 组件构建过程中遇到的问题与解决思路

    在 GearCase 开源项目构建 Table 组件的过程中.遇到了各式各样的问题,最后尝试了各种方法去解决这些问题. 遇到的部分问题 checkbox 的全选和半选问题 table 组件的排序请求方 ...

  4. Codeforces1084 | Round526Div2 | 瞎讲报告

    目录 A. The Fair Nut and Elevator B.Kvass and the Fair Nut C.The Fair Nut and String D.The Fair Nut an ...

  5. MariaDB远程连接问题

    MariaDB在设置完通过Navicat Premium远程连接账号验证通过,但是无法正常使用工具的功能,只能使用sql语句查询,但是通过控制台命令功能正常. 经过修改账号权限,添加新用户等功能都无法 ...

  6. 微软职位内部推荐-SW Engineer II for Enterprise Platform

    微软近期Open的职位: Job posting title: SDE II Location: China, Beijing Group Overview Discovery & Colla ...

  7. vue ,v-for循环对象,不是深度克隆? 数据改变了但是页面元素没有更新。问题解决

    <div id="app"> <ul > <li v-for="(val,key,idx) in list" > {{key ...

  8. Linux 基础入门第一次实验笔记

    第一节.实验介绍 本节主要介绍 Linux 的历史,Linux 与 Windows 的区别等入门知识.如果你已经有过充分的了解,可以跳过本节,直接进入下一个实验. 一.Linux 为何物 Linux ...

  9. C++:new&delete

    一.new的浅析 在C++中,new主要由三种形式:new operator.operator new和placement new • new operator new operator即一些C++书 ...

  10. Task 6.2站立会议一

    今天大家把这两天查的资料都拿出来整合到了一起,并仔细分析了其中的联系和区别. 因为大家每个人的思路都不一样,有各种各样的想法和不同的意见,所以最终统一意见是很难的一个过程.开始大家认我们可以做一个单独 ...