BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆
Description
已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。
Input
Output
输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)
题解: 用堆维护前 $k$ 远的距离,依次枚举每个点来更新即可.
#include<bits/stdc++.h>
#define ll long long
#define maxn 200000
#define inf 100000000000000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
#define mid ((l+r)>>1)
using namespace std;
void debug()
{
printf("no_problem\n");
}
int n,K,d,root;
void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
priority_queue<ll,vector<ll>, greater<ll> >Q;
struct Node
{
int p[2],ch[2],minv[2],maxv[2];
}t[maxn],T;
bool cmp(Node a,Node b)
{
return a.p[d]==b.p[d]?a.p[d^1]<b.p[d^1]:a.p[d]<b.p[d];
}
void pushup(int x,int y)
{
t[x].minv[0]=min(t[x].minv[0],t[y].minv[0]);
t[x].maxv[0]=max(t[x].maxv[0],t[y].maxv[0]);
t[x].minv[1]=min(t[x].minv[1],t[y].minv[1]);
t[x].maxv[1]=max(t[x].maxv[1],t[y].maxv[1]);
}
int build(int l,int r,int o)
{
d=o;
nth_element(t+l,t+mid,t+1+r,cmp);
t[mid].minv[0]=t[mid].maxv[0]=t[mid].p[0];
t[mid].minv[1]=t[mid].maxv[1]=t[mid].p[1];
t[mid].ch[0]=t[mid].ch[1]=0;
if(mid>l)
{
t[mid].ch[0]=build(l,mid-1,o^1);
pushup(mid,t[mid].ch[0]);
}
if(r>mid)
{
t[mid].ch[1]=build(mid+1,r,o^1);
pushup(mid,t[mid].ch[1]);
}
return mid;
}
ll sq(ll x)
{
return x * x;
}
ll getmax(int x)
{
ll ans=0;
for(int i=0;i<2;++i)
{
ans+=max( sq(1ll*(t[x].minv[i]-T.p[i])), sq(1ll*(t[x].maxv[i]-T.p[i])) );
}
return ans;
}
void solve(int x,int x1,int y1)
{
ll dn=getmax(x), cur=sq(1ll*(t[x].p[0]-x1)) + sq(1ll*(t[x].p[1]-y1));
if(dn <= Q.top()) return;
if(cur>Q.top())
{
Q.pop();
Q.push(cur);
}
ll dl=lson ? getmax(lson) : -inf;
ll dr=rson ? getmax(rson) : -inf;
if(dl>dr)
{
if(dl > Q.top()) solve(lson, x1, y1);
if(dr > Q.top()) solve(rson, x1, y1);
}
else
{
if(dr > Q.top()) solve(rson, x1, y1);
if(dl > Q.top()) solve(lson, x1, y1);
}
}
int main()
{
// setIO("input");
scanf("%d%d",&n,&K);
K<<=1;
for(int i=1;i<=n;++i) scanf("%d%d",&t[i].p[0],&t[i].p[1]);
root=build(1,n,0);
for(int i=1;i<=K;++i) Q.push(0);
for(int i=1;i<=n;++i)
{
T.p[0]=t[i].p[0];
T.p[1]=t[i].p[1];
solve(root,T.p[0],T.p[1]);
}
printf("%lld\n",Q.top());
return 0;
}
BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆的更多相关文章
- BZOJ 4520: [Cqoi2016]K远点对
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 638 Solved: 340[Submit][Status ...
- 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 ...
- [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.接下来 ...
- 【52.55%】【BZOJ 4520】K远点对
Time Limit: 30 Sec Memory Limit: 512 MB Submit: 588 Solved: 309 [Submit][Status][Discuss] Descript ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- P4357 [CQOI2016]K远点对(KDTree)
传送门 又一次产生了KDTree本质就是爆搜的感觉-- 大概就类似于p4169,只不过是从最近点对变成了第\(k\)远点对 我们开一个小根堆,里面放\(k\)个元素,起初全为\(0\),然后每一次都把 ...
随机推荐
- [Vue @Component] Pass Props to Vue Functional Templates
Functional templates allow you to create components consisting of only the template tag and exposing ...
- HLJU 1188 Matrix (二维树状数组)
Matrix Time Limit: 4 Sec Memory Limit: 128 MB Description 给定一个1000*1000的二维矩阵,初始矩阵中每一个数都为1,然后为矩阵有4种操 ...
- DB9针型:RS485输出信号及接线端子引脚分配
下图所看到的.DB9针型RS485输出信号及接线端子引脚分配. 此DB9针型与 标准 RS232 or RS485 DB9定义有所不同,下图中的DB9针型说明仅是针对USB转485DB9接口. wat ...
- swift 2.0 语法 常量变量
import UIKit /*: 常量变量 * 常量: let * 变量: var 完整格式: * 修饰符(let/var) 常量/变量名称: 数据类型 */ let number: Int var ...
- ios 使用第三方框架注意
在ios中使用第三方类库 在项目开发中经常会用到一些第三方类库,通常有两种方法来做到:一种方法是直接把所有的.h和.m文件复制到项目中:另一种方法是把.xcodeproj拖到项目中生成静 ...
- ORACLE 11G 怎样改动 awr 的保留期限小于8天
ORACLE 11G 怎样改动 awr 的保留期限小于8天 Oracle Database 11g 默认具备一个系统定义的Moving Window Baseline,该基线相应于 AWR 保留 ...
- java分页之页面分页
import java.util.ArrayList; import java.util.List; /** * * @author cheney * * @date Aug 31, 2012 */ ...
- git命令颜色设置
+ $ git config --global color.status auto + $ git config --global color.diff auto + $ git config --g ...
- go语言笔记——多值函数,本质上和nodejs的回调很像,不过nodejs是回调的第一个参数是err,而golang里是第二个!
5.2 测试多返回值函数的错误 Go 语言的函数经常使用两个返回值来表示执行是否成功:返回某个值以及 true 表示成功:返回零值(或 nil)和 false 表示失败(第 4.4 节).当不使用 t ...
- 洛谷P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...