题目链接:http://poj.org/problem?id=3241

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 N objects 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.

题目大意:给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)的更多相关文章

  1. poj 3241 Object Clustering (曼哈顿最小生成树)

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

  2. POJ 3241 Object Clustering 曼哈顿最小生成树

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

  3. R语言画全基因组关联分析中的曼哈顿图(manhattan plot)

    1.在linux中安装好R 2.准备好画曼哈顿图的R脚本即manhattan.r,manhattan.r内容如下: #!/usr/bin/Rscript #example : Rscript plot ...

  4. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  5. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  6. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  7. poj 3335 Rotating Scoreboard(半平面交)

    Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 25 ...

  8. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  9. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

随机推荐

  1. HTML5 本地存储 LocalStorage

    说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就4KB的样子,而且IE6只支持每个域名20个co ...

  2. jquery rotate

    网上发现一个很有意思的jQuery旋转插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .Opera 9 .Google Chrome,高级浏览器下 ...

  3. Scala Tail Recursion (尾递归)

    Scala对尾递归进行了优化,甚至提供了专门的标注告诉编译器需要进行尾递归优化.不过这种优化仅限于严格的尾递归,间接递归等情况,不会被优化. 尾递归的概念 递归,大家都不陌生,一个函数直接或间接的调用 ...

  4. qmake理解(还可以加入Lex Yacc文件)

    关于qmake,好一段时间令我一头雾水,不知道用来干嘛的,只知道怎么用,而且也只懂那么一两个命令,详细看过资料以后整理如下: 1.首先,感性的认识是,qmake可以利用源文件(包括头文件h,实现文件c ...

  5. C#中Dynamic关键字

    dynamic关键字和动态语言运行时(DLR)是.Net 4.0中新增的功能. 什么是"动态"? 编程语言有时可以划分为静态类型化语言和动态类型化语言.C#和Java经常被认为是静 ...

  6. python装饰器方法

    前几天向几位新同事介绍项目,被问起了@login_required的实现,我说这是django框架提供的装饰器方法,验证用户是否登录,只要这样用就行了,因为自己不熟,并没有做过多解释. 今天查看dja ...

  7. RTSP 协议分析

    RTSP 协议分析1.概述: RTSP(Real Time Streaming Protocol),实时流传输协议,是TCP/IP协议体系中的一个应用层协议,由哥伦比亚大学.网景和RealNetwor ...

  8. 如何迁移测试的MAGENTO到正式运行的MAGENTO

    或者这个题目叫做如何改变MAGENTO的运行目录 第一,我们需要在PHPMYADMIN备份整个MAGENTO的数据.备份数据并且存为.SQL. 在这儿假设你的测试网站在http://dev.site. ...

  9. Android笔记:百度地图与高德地图坐标转换问题

    安卓项目使用了百度地图的定位SDK,web端使用的也是百度地图, 后来发现界面显示百度地图不如高德效果好,web改用高德地图,原本的百度地图坐标是可以直接使用的,由于高德和百度地图的坐标系不一致 要如 ...

  10. Mac废纸篓 不能完全清空的有效解决方法

    摘自:http://www.neonan.com/articles/5582 对于苹果用户来说, 可能都会遇到过这样的情况(尽管这种情况是不常出现的!), Mac OS X系统突然出现一些顽固的文件家 ...