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

Description

We have (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply the model, each object has 2 indexes a and b (ab ≤ 500). The resemblance of object i and object j is defined by dij = |a- aj| + |b- bj|, and then we say i is dij resemble to j. Now we want to find the minimum value of X, so that we can classify the Nobjects into K (< N) groups, and in each group, one object is at most X resemble to another object in the same group, i.e, for every object i, if i is not the only member of the group, then there exists one object j (i ≠ j) in the same group that satisfies dij ≤ X

Input

The first line contains two integers N and K. The following N lines each contain two integers a and b, which describe a object.

Output

A single line contains the minimum X.

Sample Input

6 2
1 2
2 3
2 2
3 4
4 3
3 1

Sample Output

2

题目大意:

(在平面坐标第一象限)给你n个整点,A(x1,y1)和B(x2,y2)两点之间的距离定义为|x1-x2|+|y1-y2|,求最小生成树第k大边。

曼哈顿最小生成树经典题。

首先这道题条件给的不充分啊:点都在第一象限(不包括坐标轴),没有重(chong)点。

大致理一下思路:

首先,如果将点两两相连,边数是O(n^2)的。但并非所有的边都有用。网上有很多相关的证明,可以证得,只需对一二象限均分的四个部分分别求最近的点加边。

其次,怎么处理呢。先考虑y轴左偏45度的那一块。先对y-x离散化,再对x排序(排序函数要注意,因为要把边界算上),这样每次查询y-x和x比当前点大的点中最小值就好了(用线段树)。这一块实现起来确实比较麻烦,详见代码。

再次,其余三个部分通过坐标变换就可以套用第一部分的处理方式了。(x,y)(y,x)(-y,x)(x,-y)。

最后,跑个kruskal就ok了。

#include<cstdio>
#include<algorithm> using namespace std; const int maxn=;
const int inf=; struct tdot
{
int x,y;
int num;//每个点的序号
int dis;//离散化后的值
};
tdot dot[maxn+]; //x
int cmp1(tdot a,tdot b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
} //y-x
int cmp2(tdot a,tdot b)
{
if(a.y-a.x==b.y-b.x)
return a.x<b.x;
return a.y-a.x<b.y-b.x;
} //离散化及预处理
void discrete(int n)
{
sort(dot+,dot+n+,cmp2);
for(int i=;i<=n;i++)
dot[i].dis=i;
sort(dot+,dot+n+,cmp1);
} struct ttree
{
int l,r;
int num;
int mmin;
};
ttree tree[maxn*+]; void pushup(int x)
{
if(tree[x].l==tree[x].r)
return;
if(tree[x*].mmin<=tree[x*+].mmin)
tree[x].mmin=tree[x*].mmin,tree[x].num=tree[x*].num;
else
tree[x].mmin=tree[x*+].mmin,tree[x].num=tree[x*+].num;
} void build(int x,int l,int r)
{
tree[x].l=l;tree[x].r=r;
tree[x].mmin=inf;
tree[x].num=;
if(l<r)
{
int mid=(l+r)/;
build(x*,l,mid);
build(x*+,mid+,r);
}
} void modify(int x,int pos,int mmin,int num)
{
if(tree[x].l==tree[x].r)
tree[x].mmin=mmin,tree[x].num=num;
else
{
int mid=(tree[x].l+tree[x].r)/;
if(pos<=mid)
modify(x*,pos,mmin,num);
else
modify(x*+,pos,mmin,num);
pushup(x);
}
} struct tret
{
int mmin,num;
}; tret query(int x,int l,int r)
{
tret ret;
ret.mmin=inf;ret.num=;
if(l<=tree[x].l&&r>=tree[x].r)
{
ret.mmin=tree[x].mmin;
ret.num=tree[x].num;
return ret;
}
int mid=(tree[x].l+tree[x].r)/;
if(l<=mid)
{
tret ret0=query(x*,l,r);
if(ret.mmin>ret0.mmin)
ret.mmin=ret0.mmin,ret.num=ret0.num;
}
if(r>mid)
{
tret ret0=query(x*+,l,r);
if(ret.mmin>ret0.mmin)
ret.mmin=ret0.mmin,ret.num=ret0.num;
}
return ret;
} struct tedge
{
int a,b;
int w;
};
tedge edge[maxn*+];
int cnt=; //加边
void addedge(int n)
{
build(,,n);
for(int i=n;i>=;i--)
{
tret ret=query(,dot[i].dis,n);
if(ret.num!=)
{
edge[cnt].a=dot[i].num;
edge[cnt].b=ret.num;
edge[cnt++].w=ret.mmin-dot[i].x-dot[i].y;
}
modify(,dot[i].dis,dot[i].x+dot[i].y,dot[i].num);
}
} //kruskal用比较函数
int cmp(tedge a,tedge b)
{
return a.w<b.w;
} int fa[maxn+]; int father(int x)
{
if(fa[x]==x)
return x;
return fa[x]=father(fa[x]);
} int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d%d",&dot[i].x,&dot[i].y);
dot[i].num=i;
} discrete(n);
addedge(n); for(int i=;i<=n;i++)
swap(dot[i].x,dot[i].y);
discrete(n);
addedge(n); for(int i=;i<=n;i++)
dot[i].x*=-;
discrete(n);
addedge(n); for(int i=;i<=n;i++)
swap(dot[i].x,dot[i].y);
discrete(n);
addedge(n); //kruskal
for(int i=;i<=n;i++)
fa[i]=i;
sort(edge+,edge+cnt,cmp);
for(int i=,j=;i<cnt;i++)
{
int fx=father(edge[i].a);
int fy=father(edge[i].b);
if(fx!=fy)
{
fa[fx]=fy;
j++;
}
if(j==n-k)
{
printf("%d\n",edge[i].w);
break;
}
} return ;
}

PS:看别人的解题报告只是一种学习的途径。代码的实现还是要靠自己写。这两者的深度是大不相同的,有些东西只有在自己写代码的时候才能体会到。这道题前前后后写了三遍,终于过了,开心。

poj 3241 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 曼哈顿最小生成树

    题意:转换一下就是求曼哈顿最小生成树的第n-k条边 参考:莫涛大神的论文<平面点曼哈顿最小生成树> /* Problem: 3241 User: 96655 Memory: 920K Ti ...

  3. POJ 3241 Object Clustering(Manhattan MST)

    题目链接:http://poj.org/problem?id=3241 Description We have N (N ≤ 10000) objects, and wish to classify ...

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

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

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

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

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

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

  7. 【Poj3241】Object Clustering

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

  8. 【poj3241】 Object Clustering

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

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

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

随机推荐

  1. 更新centos7的kernel

    现在安装的centos7 的内核是3.10的, 机器已经联网,可以直接利用包管理工具更新,需要注意的是现在3.0以上的内核引入了签名机制,需要导入签名的key,参考步骤如下: 1.导入keyrpm - ...

  2. canvas入门,就是这个feel!

    钙素 Canvas 是在HTML5中新增的标签用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图.也就是说我们将通过JS完成画图而不是css. canva ...

  3. java.lang.String 的 + 号操作到底做了什么事情?

    前言  在之前的面试经历中,对于String的考察还是挺频繁的,大致考察以下几个知识点: String 常量池 new String() == 和 equals 的区别 native 方法 Strin ...

  4. JAVA _____Scanner用法

    今天就来说一说Scanner用法,以前我在学C的时候记得第一天学的是很普遍的HelloWord的输出,JAVA中的输出是这样子的, public class ScannerWriter { publi ...

  5. JSONPath入门之Snack3篇

    Snack3 for java 一个微型JSON框架 基于jdk8,60kb.有序列化反序列化.解析和转换.支持 Json path 查询. <dependency> <groupI ...

  6. 实战webpack系列02

    02. 开始使用webpack 1.1.安装 Webpack可以使用npm安装,新建一个空的练习文件夹(此处命名为webpack sample project),在终端中转到该文件夹后执行下述指令就可 ...

  7. python json序列化与反序列化操作

    python json序列化与反序列化操作 # dumps() dict-->str 序列化 # loads() str---dict 反序列化 result1 = json.dumps({'a ...

  8. 【Android - 组件】之Activity的启动模式

    Activity的启动模式目前有四种:standard.singleTop.singleTask 和 singleInstance. 1.standard standard 是标准模式,也是系统的默认 ...

  9. linux下创建mysql用户和数据库,并绑定

    Linux下输入命令: mysql -uroot -proot123 进入mysql后输入: 查看目前有哪些数据库存在:mysql> SHOW DATABASES; 创建数据库:create s ...

  10. 【Python还能干嘛】爬取微信好友头像完成马赛克拼图(千图成像)~

    马赛克拼图 何谓马赛克拼图(千图成像),简单来说就是将若干小图片平凑成为一张大图,如下图路飞一样,如果放大看你会发现里面都是一些海贼王里面的图片. Our Tragets 爬取所有微信好友的头像