题意:转换一下就是求曼哈顿最小生成树的第n-k条边

参考:莫涛大神的论文《平面点曼哈顿最小生成树》

/*
Problem: 3241 User: 96655
Memory: 920K Time: 94MS
Language: C++ Result: Accepted
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
const int maxm=;
int n,k,p;
struct Point
{
int x,y,id;
bool operator<(const Point &e)const
{
if(x==e.x)return y<e.y;
return x<e.x;
}
} point[maxn];
struct Edge
{
int u,v,w;
bool operator<(const Edge &e)const
{
return w<e.w;
}
} edge[maxn*];
void addedge(int a,int b,int l,int r)
{
++p;
edge[p].u=a;
edge[p].v=b;
edge[p].w=abs(l-r);
}
struct Node
{
int len,id;
} node[(maxm+)<<];
void pushup(int rt)
{
node[rt].len=min(node[rt*].len,node[rt*+].len);
if(node[rt].len==node[rt*].len)node[rt].id=node[rt*].id;
else node[rt].id=node[rt*+].id;
}
void change(int rt,int l,int r,Point tt)
{
if(l==r)
{
node[rt].len=tt.x+tt.y;
node[rt].id=tt.id;
return;
}
int pos=tt.y-tt.x+;
int m=(l+r)>>;
if(pos<=m)change(rt*,l,m,tt);
else change(rt*+,m+,r,tt);
pushup(rt);
}
Node query(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
return node[rt];
int m=(l+r)>>;
if(y<=m)return query(rt*,l,m,x,y);
else if(x>m)return query(rt*+,m+,r,x,y);
else
{
Node t1,t2;
t1=query(rt*,l,m,x,y);
t2=query(rt*+,m+,r,x,y);
if(t1.len<t2.len)return t1;
else return t2;
}
}
int fa[maxn];
int find(int x)
{
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
void init()
{
for(int i=; i<(maxm<<); ++i)
node[i].len=INF,node[i].id=-;
}
void build()
{
sort(point+,point++n);
init();
for(int i=n; i>; --i)
{
int s=point[i].x+point[i].y;
Node t=query(,,maxm,point[i].y-point[i].x+,maxm);
if(t.id!=-)
addedge(point[i].id,t.id,s,t.len);
change(,,maxm,point[i]);
} }
int solve()
{
sort(edge+,edge+p+);
int cnt=;
for(int i=; i<=p; ++i)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx!=fy)
{
fa[fy]=fx;
++cnt;
if(cnt==n-k)return edge[i].w;
}
}
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; ++i)
scanf("%d%d",&point[i].x,&point[i].y),point[i].id=i;
for(int i=; i<=n; ++i)
fa[i]=i;
p=;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y,swap(point[i].x,point[i].y);
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
printf("%d\n",solve());
return ;
}

POJ3241 Object Clustering 曼哈顿最小生成树的更多相关文章

  1. POJ 3241 Object Clustering 曼哈顿最小生成树

    Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...

  2. POJ3241 Object Clustering(最小生成树)题解

    题意:求最小生成树第K大的边权值 思路: 如果暴力加边再用Kruskal,边太多会超时.这里用一个算法来减少有效边的加入. 边权值为点间曼哈顿距离,那么每个点的有效加边选择应该是和他最近的4个象限方向 ...

  3. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  4. 【Poj3241】Object Clustering

    Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...

  5. poj 3241 Object Clustering (曼哈顿最小生成树)

    Object Clustering Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 2640   Accepted: 806 ...

  6. 【poj3241】 Object Clustering

    http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...

  7. POJ 3241Object Clustering曼哈顿距离最小生成树

    Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into several ...

  8. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组

    2177: 曼哈顿最小生成树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 190  Solved: 77[Submit][Status][Discu ...

  9. BZOJ 2177: 曼哈顿最小生成树

    Sol 考了好几次曼哈顿最小生成树,然而一直不会打...这次终于打出来了...神tm调试了2h...好蛋疼... 首先曼哈顿最小生成树有个结论就是讲它每45度分出一个象限,对于每个点,只与每个象限中离 ...

随机推荐

  1. 【BZOJ 1087】[SCOI2005]互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  2. python学习笔记17(动态类型)

    动态类型 在我们接触的对象中,有一类特殊的对象,是用于存储数据的,常见的该类对象包括各种数字,字符串,表,词典.在C语言中,我们称这样一些数据结构为变量,而在Python中,这些是对象. 对象是储存在 ...

  3. CSS3圆角气泡框,评论对话框

    <title>CSS3圆角气泡框,评论对话框</title> <style> body { ; ; font:1em/1.4 Cambria, Georgia, s ...

  4. csu 1303 Decimal (数论题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1303 1303: Decimal Time Limit: 1 Sec  Memory Limit: ...

  5. springside springmvc 的一个SB问题

    <form  id="inputForm" modelAttribute="order" action="${ctx}/myorder/orde ...

  6. jQuery,javascript获得网页的高度和宽度

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  7. Fiddler 日志

    Fiddler 日志(Logging) 在开发扩展插件及编写FiddlerScript时对调试程序非常有用. 1.输出日志 在FiddlerScript脚本中,你可以这样输出输出日志: Fiddler ...

  8. Oracle 学习笔记(一)

    1.连接数据库命令: conn 用户名/密码,当用特权身份连接时,要加上as sysdba 2.修改密码: passw(ord),如果要修改其他人的密码,需要用sys或者system登录 3.显示当前 ...

  9. PHP开发工具介绍之zendStudio

    1.PHP开发工具介绍之zendStudio 下载:进入官网:http://www.zend.com/en/products/studio 选择下载安装 注意这里的工作空间要和你Apache的工作目录 ...

  10. SaaS系列介绍之二: SaaS介绍

    1 引言 横看成岭侧成峰,远近高低各不同. 不识庐山真面目, 只缘身在此山中.                                                  ________苏轼, ...