老oj3444 && Pku3241 Object Clustering
Description
We have N (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 (a, b ≤ 500). The resemblance of object i and object j is defined by dij = |ai - aj| + |bi - 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 N objects into K (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 又是一道曼哈顿距离最小生成树。。。
code:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 100005
#define inf 1061109567
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
struct Point{
int x,y,d,id;
}point[maxn],tmp[maxn];
bool cmp1(Point a,Point b){
if (a.x!=b.x) return a.x>b.x;
return a.y>b.y;
}
int calc(Point a,Point b){return abs(a.x-b.x)+abs(a.y-b.y);}
int n,m,k,ans;
struct DATA{
int val,pos;
void init(){val=inf,pos=-;}
void update(DATA b){if (val>b.val) val=b.val,pos=b.pos;}
};
int d[maxn],cntd;
struct bit{
#define lowbit(x) ((x)&(-(x)))
DATA node[maxn];
void init(){for (int i=;i<=cntd;i++) node[i].init();}
void insert(int x,DATA p){for (int i=cntd-x+;i<=cntd;i+=lowbit(i)) node[i].update(p);}
int query(int x){
DATA ans; ans.init();
for (int i=cntd-x+;i;i-=lowbit(i)) ans.update(node[i]);
return ans.pos;
}
}T;
struct Edge{
int u,v,c;
}edge[maxn<<];
bool cmp2(Edge a,Edge b){return a.c<b.c;}
void prepare(){
for (int i=;i<=n;i++) d[i]=point[i].d=point[i].y-point[i].x;
sort(d+,d+n+),cntd=unique(d+,d+n+)-d-;
for (int i=;i<=n;i++) point[i].d=lower_bound(d+,d+cntd+,point[i].d)-d;
sort(point+,point+n+,cmp1),T.init();
for (int i=;i<=n;i++){
int u=point[i].id,v=T.query(point[i].d);
if (v!=-) edge[++m]=(Edge){u,v,calc(tmp[u],tmp[v])};
T.insert(point[i].d,(DATA){point[i].x+point[i].y,u});
}
}
int fa[maxn];
int find(int x){return x==fa[x]?fa[x]:fa[x]=find(fa[x]);}
int main(){
read(n),read(k);
for (int i=;i<=n;i++) read(point[i].x),read(point[i].y),point[i].id=i;
for (int i=;i<=n;i++) tmp[i]=point[i]; prepare();
for (int i=;i<=n;i++) point[i].x=tmp[i].y,point[i].y=tmp[i].x,point[i].id=i; prepare();
for (int i=;i<=n;i++) point[i].x=-tmp[i].y,point[i].y=tmp[i].x,point[i].id=i; prepare();
for (int i=;i<=n;i++) point[i].x=tmp[i].x,point[i].y=-tmp[i].y,point[i].id=i; prepare();
sort(edge+,edge+m+,cmp2);
for (int i=;i<=n;i++) fa[i]=i;
for (int i=,cnt=n;i<=m&&cnt>k;i++) if (find(edge[i].u)!=find(edge[i].v))
cnt--,ans=max(ans,edge[i].c),fa[find(edge[i].u)]=find(edge[i].v);
printf("%d\n",ans);
return ;
}
老oj3444 && Pku3241 Object Clustering的更多相关文章
- POJ 3241 Object Clustering 曼哈顿最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...
- 【Poj3241】Object Clustering
Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...
- poj 3241 Object Clustering (曼哈顿最小生成树)
Object Clustering Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 2640 Accepted: 806 ...
- POJ 3241 Object Clustering(Manhattan MST)
题目链接:http://poj.org/problem?id=3241 Description We have N (N ≤ 10000) objects, and wish to classify ...
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- 【poj3241】 Object Clustering
http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...
- POJ3241 Object Clustering 曼哈顿最小生成树
题意:转换一下就是求曼哈顿最小生成树的第n-k条边 参考:莫涛大神的论文<平面点曼哈顿最小生成树> /* Problem: 3241 User: 96655 Memory: 920K Ti ...
- POJ3241 Object Clustering(最小生成树)题解
题意:求最小生成树第K大的边权值 思路: 如果暴力加边再用Kruskal,边太多会超时.这里用一个算法来减少有效边的加入. 边权值为点间曼哈顿距离,那么每个点的有效加边选择应该是和他最近的4个象限方向 ...
- POJ 3241 曼哈顿距离最小生成树 Object Clustering
先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...
随机推荐
- firefox如何restart重启
1.开发者工具栏可以对firefox进行重启,不是关闭firefox又打开那种重启
- oracle DG 主备切换语句整理
今日花了一下午时间进行了Oracle DataGuard的切换练习,参考了网上好多文章,最后将一些语句进行摘录,以备以后查询使用.之后有时间会带来Oracle DG的搭建和切换全过程文章. DataG ...
- HTML5 File api 实现断点续传
目前市场上大多数的网站的断点上传都是需要安装浏览器插件的,本文就针对高级浏览器的环境下,通过HTML5 File api实现断点上传进行说明 一.实现文件多选 HTML5的<input>新 ...
- C# 保存窗口为图片(保存纵断面图)
源代码例如以下: #region 保存纵断面截图 private void button_save_Click(object sender , EventArgs e) { SaveFileDialo ...
- Android 颜色渲染(二) 颜色区域划分原理与实现思路
版权声明:本文为博主原创文章,未经博主允许不得转载. 上一篇讲到颜色选择器,该demo不能选择黑白或者具体区间颜色,这是为什么呢,还是要从原理部分讲起,首先看一下两张图: 图1 ...
- win主机用web.config和httpd.ini实现301重定向
当你准备好好看这篇文章的时候,你应该已经知道了301重定向的作用与意义了,那么这里就不多加解释了. 那么我唯一想提的就是关于域名带与不带www的区别,并且301重定在其中的意义,详情:域名带与不带ww ...
- java中调用js脚本
JDK1.6加入了对Script(JSR223)的支持.这是一个脚本框架,提供了让脚本语言来访问Java内部的方法.你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本.这个脚本API允许你为脚 ...
- ASP.NET 动态属性筛选和分页绑定
分页控件为:AspNetPager.dll 我们先建立一个产品属性名称表 CREATE TABLE ProductAttr ( ,) NOT NULL primary key, [ParentID] ...
- NP-难题
所谓NP-难题,在给定的一个信息系统中,假设研究对象书目为m,属性书目为n,则要考察的属性集P的一个子集是否为最小子集,要进行n*m*m次的比较.而n个属性可构成2的n次方个子集,这些子集都有可能是最 ...
- WPF FindName()没找到指定名称的元素
1.FindName()说明,可以用来获取已经注册名称的元素或标签 // // 摘要: // 查找具有提供的标识符名的元素. // // 参数: // name: // 所请求元素的名称. // // ...