题目链接: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. Python中什么是set、更新、遍历set和set的特点

    dict的作用是建立一组 key 和一组 value 的映射关系,dict的key是不能重复的. 有的时候,我们只想要 dict 的 key,不关心 key 对应的 value,目的就是保证这个集合的 ...

  2. rpc rmi http

    1.RPC与RMI (1)RPC 跨语言,而 RMI只支持Java. (2)RMI 调用远程对象方法,允许方法返回 Java 对象以及基本数据类型,而RPC 不支持对象的概念,传送到 RPC 服务的消 ...

  3. Cocos2d-JS目录说明

    frameworks---- 引擎所在,包含两个文件夹cocos2d-html5 和js-bindings.前者是html5引擎,后者是-x的引擎,外部接口是js写,但基础模块却是cpp来实现. sa ...

  4. [LeetCode]题解(python):083 - Remove Duplicates from Sorted List

    题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, d ...

  5. 登录不到phpmyadmin

    或许出现以下错误情况:phpmyadmin:#1045 无法登录 MySQL 服务器.Access denied for user ‘root’@’localhost’ (using password ...

  6. 根据 字数 确定 UI控件高度

    //字体 textLabel.font = [UIFont systemFontOfSize:13]; CGFloat labelWidth = [UIScreen mainScreen].bound ...

  7. JQuery 方法简写

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. Selenium2学习-033-WebUI自动化实战实例-031-页面快照截图应用之二 -- 区域截图

    我在之前的文章中曾给出浏览器显示区域截图的方法,具体请参阅 .或许,有些小主已经想到了,每次都获取整个显示区域的截图存储,那么经过一段时间后,所使用的图片服务器的容量将会受到极大的挑战,尤其是在产品需 ...

  9. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  10. Fast and Robust Hand Tracking Using Detection-Guided Optimization

    http://handtracker.mpi-inf.mpg.de/projects/FastHandTracker/