题意:转换一下就是求曼哈顿最小生成树的第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. 0ffice365 Calendar API

    Calendar REST API in Office 365 APIs Preview http://msdn.microsoft.com/EN-US/library/office/dn792114 ...

  2. Java中的break与continue区别

    break跳出当前循环执行循环下面的程序, 如果break出现在嵌套循环的内层循环, 则break语句只会跳出当前层的循环; 当程序执行到continue时时, 则跳过本次循环程序重新回到循环开始继续 ...

  3. 使用Sqlserver事务发布实现数据同步

    事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进 的.这里以sqlserver2008的事务发布功能为例,对发 ...

  4. c++ 异常处理 assert | try

    #include <iostream> #include <cassert> using namespace std; int main() { ; assert(i == ) ...

  5. PAT-乙级-1037. 在霍格沃茨找零钱(20)

    1037. 在霍格沃茨找零钱(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 如果你是哈利·波特迷,你会知 ...

  6. 快速配置Ehcache

    1. 编写ehcache.xml文件,将该文件放置于classpath路径下.代码如下: <?xml version="1.0" encoding="UTF-8&q ...

  7. 16进制字符串转数字(C/C++,VB/VB.net,C#)

    这个问题看是很简单,但是在不同语言中实现的方式却千差万别,如果不知道方法,还真是麻烦,我就是在C#中遇到该问题,让我费了很大的周折,才在msdn查到. 一.16进制字符串转数字      1.C/C+ ...

  8. 跌跌撞撞,用MAVEN看图说话的编译了一个JAR出来

    URL: http://www.yiibai.com/maven/create-a-java-project-with-maven.html mvn archetype:generate -Dgrou ...

  9. java List 去重(两种方式)

    方法一: 通过Iterator 的remove方法 Java代码  public void testList() { List<Integer> list=new ArrayList< ...

  10. Web开发的绝美网站

    http://paranimage.com/ http://sixrevisions.com/graphics-design/