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 ...
随机推荐
- correctly handle PNG transparency in Win IE 5.5 & 6.
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var arVersion = ...
- eclipse Maven -->web project
http://blog.chinaunix.net/uid-26959955-id-3248053.html http://blog.csdn.net/wilsonke/article/details ...
- docker kubernetes--
http://kubernetes.io/docs/getting-started-guides/docker/ https://hub.docker.com/r/solsson/hyperkube- ...
- 介绍UDF,以及完成大小写的转换
一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...
- SQLite.net发布后找不到"SQLite.Interop.dll"的问题
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx40-static-binary-b ...
- Provisioning Profile
什么是Provisioning Profile? 从字面翻译,Provisioning Profile就是配置文件的意思,它在开发者账号体系中所扮演的角色也是配置和验证的作用.如果你有开发者账号,可以 ...
- Wordpress更换编辑器
这里我更换为KindEditor 1.下载插件 https://wordpress.org/plugins/kindeditor-for-wordpress/ 2.启动插件 3.在 设置 – Kind ...
- Magento去掉价格的小数点
magento的默认情况,价格后面是有小数点的,我们来看下如何正确的来去掉小数点. 1.复制如下路径的文件 app/code/core/Mage/Directory/Model/Currency.ph ...
- 利用HTML和JS制作隔行换背景颜色的表格
1.源代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- C# RichTextBox 滚动条 滚动到最后一行
使用RichTextBox控件用于显示数据时,滚动条只停留在开头,而我希望能够一直更新,显示最后一行的内容.解决方法记录于此. 转载自以下链接: http://blog.csdn.net/xelone ...