POJ 3241 Object Clustering(Manhattan MST)
题目链接:http://poj.org/problem?id=3241
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.
题目大意:给n个点,两个点之间的距离为曼哈顿距离。要求把n个点分成k份,每份构成一个连通的子图(树),要求最大边最小。求最大边的最小值。
思路:实际上就是求平面上曼哈顿距离的最小生成树的第k大边(即减掉最大的k-1条边构成k份)。
资料:曼哈顿MST。复杂度O(nlogn)。
http://wenku.baidu.com/view/1e4878196bd97f192279e941.html
http://blog.csdn.net/huzecong/article/details/8576908
代码(79MS):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
#define FOR(i, n) for(int i = 0; i < n; ++i) namespace Bilibili {
const int MAXV = ;
const int MAXE = MAXV * ; struct Edge {
int u, v, cost;
Edge(int u = , int v = , int cost = ):
u(u), v(v), cost(cost) {}
bool operator < (const Edge &rhs) const {
return cost < rhs.cost;
}
}; struct Point {
int x, y, id;
void read(int i) {
id = i;
scanf("%d%d", &x, &y);
}
bool operator < (const Point &rhs) const {
if(x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
}; Point p[MAXV];
Edge edge[MAXE];
int x_plus_y[MAXV], y_sub_x[MAXV];
int n, k, ecnt; int hash[MAXV], hcnt; void get_y_sub_x() {
for(int i = ; i < n; ++i) hash[i] = y_sub_x[i] = p[i].y - p[i].x;
sort(hash, hash + n);
hcnt = unique(hash, hash + n) - hash;
for(int i = ; i < n; ++i) y_sub_x[i] = lower_bound(hash, hash + hcnt, y_sub_x[i]) - hash + ;
} void get_x_plus_y() {
for(int i = ; i < n; ++i) x_plus_y[i] = p[i].x + p[i].y;
} int tree[MAXV];
int lowbit(int x) {
return x & -x;
} void update_min(int &a, int b) {
if(b == -) return ;
if(a == - || x_plus_y[a] > x_plus_y[b])
a = b;
} void initBit() {
memset(tree + , -, hcnt * sizeof(int));
} void modify(int x, int val) {
while(x) {
update_min(tree[x], val);
x -= lowbit(x);
}
} int query(int x) {
int res = -;
while(x <= hcnt) {
update_min(res, tree[x]);
x += lowbit(x);
}
return res;
} void build_edge() {
sort(p, p + n);
get_x_plus_y();
get_y_sub_x();
initBit();
for(int i = n - ; i >= ; --i) {
int tmp = query(y_sub_x[i]);
if(tmp != -) edge[ecnt++] = Edge(p[i].id, p[tmp].id, x_plus_y[tmp] - x_plus_y[i]);
modify(y_sub_x[i], i);
}
} int fa[MAXV], ans[MAXV]; int find_set(int x) {
return fa[x] == x ? x : fa[x] = find_set(fa[x]);
} int kruskal() {
for(int i = ; i < n; ++i) fa[i] = i;
sort(edge, edge + ecnt);
int acnt = ;
for(int i = ; i < ecnt; ++i) {
int fu = find_set(edge[i].u), fv = find_set(edge[i].v);
if(fu != fv) {
ans[acnt++] = edge[i].cost;
fa[fu] = fv;
}
}
reverse(ans, ans + acnt);
return ans[k - ];
} void mymain() {
scanf("%d%d", &n, &k);
for(int i = ; i < n; ++i) p[i].read(i); build_edge();
for(int i = ; i < n; ++i) swap(p[i].x, p[i].y);
build_edge();
for(int i = ; i < n; ++i) p[i].x = -p[i].x;
build_edge();
for(int i = ; i < n; ++i) swap(p[i].x, p[i].y);
build_edge(); printf("%d\n", kruskal());
}
} int main() {
Bilibili::mymain();
}
POJ 3241 Object Clustering(Manhattan MST)的更多相关文章
- poj 3241 Object Clustering (曼哈顿最小生成树)
Object Clustering Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 2640 Accepted: 806 ...
- POJ 3241 Object Clustering 曼哈顿最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...
- R语言画全基因组关联分析中的曼哈顿图(manhattan plot)
1.在linux中安装好R 2.准备好画曼哈顿图的R脚本即manhattan.r,manhattan.r内容如下: #!/usr/bin/Rscript #example : Rscript plot ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- poj 3335 Rotating Scoreboard(半平面交)
Rotating Scoreboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6420 Accepted: 25 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
随机推荐
- (转)CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example)
CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example) tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0 ...
- extjs MVC模式的个人看法
针对一个后台管理页面是mvc模式,后台也是mvc模式下的项目,要怎么去熟悉呢? 首先以我个人的认解,先从后台的管理界面来看,会有control,model,store,view:其中先看view的代码 ...
- 【Android测试】【随笔】与 “美丽说” 测试同事交流
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5405432.html 分享者简介 雪晗,3年+测试经验,现 ...
- Solr4+IKAnalyzer的安装配置
一.下载Solr4.10.2 我们以Windows版本为例,solr-4.10.2.zip是目前最新版本,下载地址: http://www.apache.org/dyn/closer.cgi/luce ...
- 【Algorithm】堆排,C++实现
对一个数组中的元素按照顺序构建二叉树,就形成了一个(二叉)堆.(二叉树是虚拟的,并不是真的建立二叉树) 表示堆的数组A有两个重要属性:A.heapSize,表示堆里面有多少元素,数组里有多少元素在堆里 ...
- Linux上使用SMART检测硬盘
SMART(Self-Monitoring, Analysis, and Reporting Technology)是一种普及度比较高的磁盘分析检测工具,磁盘运行过程中,该工具搜集磁盘的状态参数,如型 ...
- 弹窗插件layer
layer的插件的地址:http://layer.layui.com/简单使用: layer.open({ type: , //page层 area: ['500px', '300px'], titl ...
- JQuery:JQuery添加元素
JQuery:添加元素通过 jQuery,可以很容易地添加新元素/内容.添加新的HTML内容.我们将学习用于添加新内容的四个jQuery方法: append() - 在被选元素的结尾插入内容 prep ...
- java实现读取文件内容(不同类型)
在一些项目中大量的数据经常需要从文件中读取,例如xml文件,txt文件,csv文件 1.读取本地的xml文件,需要注意对应的路径 //读取xml文件,xmlFile为读取文件的路径 DocumentB ...
- SQL-Server2008 数据库发布订阅
参考博客园文章http://www.cnblogs.com/tyb1222/archive/2011/05/31/2064944.html 添加安全数据库 更改数据库名称 SQL语句 select @ ...