老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 ...
随机推荐
- 超级好用的国际汇兑平台--Transferwise
一年的CSC留学快结束了,手里还剩了些积攒下来的美元.就国内那点博士的工资,出来一趟好不容易领了点美元可不想都给银行汇兑的手续费给吞了去. 这两天英国退欧,英镑大跌,美元有涨,是个把手里的美元寄回国换 ...
- redis.config翻译
# Redis configuration file example#redis配置文件范例 # Note on units: when memory size is needed, it is po ...
- 树莓派安装mjpg-streamer视频监控 分类: Raspberry Pi 2015-04-12 23:41 144人阅读 评论(0) 收藏
原来使用Motion在树莓派上跑1280x720分辨率的三颗摄像头.占用内存太严重,关闭诸多功能之后还是不行.故转战mjpg-streamer. 首先安装所需软件 sudo apt-get insta ...
- mysql 参数:[read_buffer_size] [sort_buffer_size] [read_rnd_buffer_size] [tmp_table_size]---图解
http://imysql.cn/2008_09_27_deep_into_mysql_sort_buffer http://my.oschina.net/realfighter/blog/36442 ...
- css 权威指南笔记(一)
零零散散接触css将近5年,俨然已经成为一个熟练工.如果不是换份工作,我不知道自己差的那么远:在qunar的转正review中我这种“知其然而不知其所以然” 的状况被标明,我才意识到我已停步不前近两年 ...
- LDAP7卸载
3 Uninstalling Directory Server Enterprise Edition This chapter provides instructions for uninstalli ...
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- ASP.NET如何发布更新
如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...
- nodejs开发环境sublime配置
前端时间使用webstorm搭建一个node.js的学习环境,感觉非常强大.不过由于其加载的速度,每次让都让我抓狂.后来我找到了一个sublime.虽说3.0以上是收费的,2.0暂时免费.官方的不对s ...
- SQLite 入门教程(二)创建、修改、删除表
一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 Trigger 等等,构成了数据库的架构 Schem ...