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 ...
随机推荐
- ASP.NET WebForm与ASP.NET MVC的不同点
ASP.NET WebForm ASP.NET MVC ASP.NET Web Form 遵循传统的事件驱动开发模型 ASP.NET MVC是轻量级的遵循MVC模式的请求处理响应的基本开发模型 ASP ...
- docker confluence
http://wuyijun.cn/shi-yong-dockerfang-shi-an-zhuang-he-yun-xing-confluence/ https://hub.docker.com/r ...
- Redis学习笔记--五种数据类型的使用场景
String 1.String 常用命令: 除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操作: 获取字符串长度 往字符串append内容 设置和获取字符串的某一 ...
- Qt的IDE开发环境(KDevelop,MonKey Studio,QDevlop,Dev-cpp,Cobras,Edyuk)
讲到Qt的IDE开发环境,本人一直在Windows下使用VC6.0 + Qt4.3.1开发程序.但转到Linux下,使用Fedora中自带的KDevelop + Qt4.3.1开发程序. 最近一直做Q ...
- spring3 mvc:方法返回值的学习
新建后台代码用以测试返回类型,在这里我新建的如下: /** * 项目名称:Spring3mvc demo * Copyright ? 2010-2012 spartacus.org.cn All Ri ...
- Magento的迁移方法
Magento有很多配置内容,比如说CMS配置页.Static Stock.多语言配置等等,所以做数据迁移很有必要性,下面就说说如何做迁移 这个技术文章是从网上整理的,不过一个很重要的点被疏忽了,我在 ...
- The Top Five Software Project Risks
Risk management (or more precisely risk avoidance) is a critical topic, but one that is often dull t ...
- [收藏夹整理]VC部分
c++多线程(一) C++11 多线程 鸡啄米:C++编程入门系列之十二(类与对象:面向对象设计的基本思想和特点) 鸡啄米:C++编程入门系列之二十一(C++程序设计必知:类的静态成员) [笔记]VS ...
- ie8兼容border-radius方法
<!doctype html><html> <head> <meta charset="utf-8" /> &l ...
- 获取dom元素的宽度和高度
一.获取css的大小 1.第一种通过内联样式 var box = document.getElementById('box'); var w = box.style.width; var h = bo ...