题目链接: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):

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 typedef long long LL;
  7 #define FOR(i, n) for(int i = 0; i < n; ++i)
  8
  9 namespace Bilibili {
 10     const int MAXV = 10010;
 11     const int MAXE = MAXV * 4;
 12
 13     struct Edge {
 14         int u, v, cost;
 15         Edge(int u = 0, int v = 0, int cost = 0):
 16             u(u), v(v), cost(cost) {}
 17         bool operator < (const Edge &rhs) const {
 18             return cost < rhs.cost;
 19         }
 20     };
 21
 22     struct Point {
 23         int x, y, id;
 24         void read(int i) {
 25             id = i;
 26             scanf("%d%d", &x, &y);
 27         }
 28         bool operator < (const Point &rhs) const {
 29             if(x != rhs.x) return x < rhs.x;
 30             return y < rhs.y;
 31         }
 32     };
 33
 34     Point p[MAXV];
 35     Edge edge[MAXE];
 36     int x_plus_y[MAXV], y_sub_x[MAXV];
 37     int n, k, ecnt;
 38
 39     int hash[MAXV], hcnt;
 40
 41     void get_y_sub_x() {
 42         for(int i = 0; i < n; ++i) hash[i] = y_sub_x[i] = p[i].y - p[i].x;
 43         sort(hash, hash + n);
 44         hcnt = unique(hash, hash + n) - hash;
 45         for(int i = 0; i < n; ++i) y_sub_x[i] = lower_bound(hash, hash + hcnt, y_sub_x[i]) - hash + 1;
 46     }
 47
 48     void get_x_plus_y() {
 49         for(int i = 0; i < n; ++i) x_plus_y[i] = p[i].x + p[i].y;
 50     }
 51
 52     int tree[MAXV];
 53     int lowbit(int x) {
 54         return x & -x;
 55     }
 56
 57     void update_min(int &a, int b) {
 58         if(b == -1) return ;
 59         if(a == -1 || x_plus_y[a] > x_plus_y[b])
 60             a = b;
 61     }
 62
 63     void initBit() {
 64         memset(tree + 1, -1, hcnt * sizeof(int));
 65     }
 66
 67     void modify(int x, int val) {
 68         while(x) {
 69             update_min(tree[x], val);
 70             x -= lowbit(x);
 71         }
 72     }
 73
 74     int query(int x) {
 75         int res = -1;
 76         while(x <= hcnt) {
 77             update_min(res, tree[x]);
 78             x += lowbit(x);
 79         }
 80         return res;
 81     }
 82
 83     void build_edge() {
 84         sort(p, p + n);
 85         get_x_plus_y();
 86         get_y_sub_x();
 87         initBit();
 88         for(int i = n - 1; i >= 0; --i) {
 89             int tmp = query(y_sub_x[i]);
 90             if(tmp != -1) edge[ecnt++] = Edge(p[i].id, p[tmp].id, x_plus_y[tmp] - x_plus_y[i]);
 91             modify(y_sub_x[i], i);
 92         }
 93     }
 94
 95     int fa[MAXV], ans[MAXV];
 96
 97     int find_set(int x) {
 98         return fa[x] == x ? x : fa[x] = find_set(fa[x]);
 99     }
100
101     int kruskal() {
102         for(int i = 0; i < n; ++i) fa[i] = i;
103         sort(edge, edge + ecnt);
104         int acnt = 0;
105         for(int i = 0; i < ecnt; ++i) {
106             int fu = find_set(edge[i].u), fv = find_set(edge[i].v);
107             if(fu != fv) {
108                 ans[acnt++] = edge[i].cost;
109                 fa[fu] = fv;
110             }
111         }
112         reverse(ans, ans + acnt);
113         return ans[k - 1];
114     }
115
116     void mymain() {
117         scanf("%d%d", &n, &k);
118         for(int i = 0; i < n; ++i) p[i].read(i);
119
120         build_edge();
121         for(int i = 0; i < n; ++i) swap(p[i].x, p[i].y);
122         build_edge();
123         for(int i = 0; i < n; ++i) p[i].x = -p[i].x;
124         build_edge();
125         for(int i = 0; i < n; ++i) swap(p[i].x, p[i].y);
126         build_edge();
127
128         printf("%d\n", kruskal());
129     }
130 }
131
132 int main() {
133     Bilibili::mymain();
134 }

View Code

转载于:https://www.cnblogs.com/oyking/p/4264750.html

POJ 3241 Object Clustering(Manhattan MST)相关推荐

  1. R语言计算曼哈顿距离(Manhattan Distance)实战:计算两个向量的曼哈顿距离、dist函数计算矩阵中两两元素的曼哈顿距离

    R语言计算曼哈顿距离(Manhattan Distance)实战:计算两个向量的曼哈顿距离.dist函数计算矩阵中两两元素的曼哈顿距离 目录 R语言计算曼哈顿距离(Manhattan Distance ...

  2. POJ 1696 Space Ant(极角排序)【计算几何】

    ACM博客_kuangbin POJ 1696 Space Ant(极角排序) Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. poj 2769 感觉♂良好 (单调栈)

    poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...

  4. Spectral clustering(谱聚类)算法的实现

    目录 1.作者介绍 2.关于谱聚类的介绍 2.1 谱聚类概述 2.2 无向权重图 2.3 邻接矩阵 2.4 相似矩阵 2.5 度矩阵 2.6 拉普拉斯矩阵 2.7 K-Means 3.Spectral ...

  5. POJ 3253 Fence Repair(修篱笆)

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

  6. 机器学习中的数学——距离定义(二):曼哈顿距离(Manhattan Distance)

    分类目录:<机器学习中的数学>总目录 相关文章: · 距离定义:基础知识 · 距离定义(一):欧几里得距离(Euclidean Distance) · 距离定义(二):曼哈顿距离(Manh ...

  7. 【论文总结】Towards Open World Object Detection(附翻译)

    Towards Open World Object Detection 开放世界的目标检测 论文地址:https://arxiv.org/abs/2103.02603 代码地址:GitHub - Jo ...

  8. Graph Signal Processing——Part I: Graphs, Graph Spectra, and Spectral Clustering (文献翻译)

    目录 目录 0.Abstract 1.Introduction 2.图形定义和属性 2.1 基本定义 2.2 一些常用的图拓扑 2.3 图及其相关矩阵的性质 3.图矩阵的谱分解(特征分解) 3.1 邻 ...

  9. POJ 3889 Fractal Streets(逼近模拟)

    $ POJ~3889~Fractal~Streets $(模拟) $ solution: $ 这是一道淳朴的模拟题,最近发现这种题目总是可以用逼近法,就再来练练手吧. 首先对于每个编号我们可以用逼近法 ...

最新文章

  1. python数据分析工具包_脑成像数据分析:Python工具包
  2. Entity Framework 与 LINQ to SQL
  3. shell判断是否为root权限(id -u != 0)
  4. myBatis的xml映射文件中传入list集合与数组做条件
  5. Linux平台kafaka安装及使用
  6. 桌面上计算机点击后,怎样设置电脑的鼠标点击后窗口(也就是桌面上点一下出来的窗口)为自己另类设计的个性窗口图案?求高手。...
  7. 联想怎么启用计算机的无线功能,IdeaCentre B3系列电脑无线连接中心的使用方法...
  8. 领域的初学者--推荐的一本书
  9. 2021 天勤率辉计算机考研(附408)
  10. 大数据分析-第十二章 Spark与数据分析
  11. 雷电模拟器 + Xposed框架 + 北京消费券
  12. 服务器上验证码不显示
  13. python 列表去重 保持顺序
  14. 基于Netty的Android局域网IP电话
  15. 度度熊与邪恶大魔王 百度之星
  16. 2018.7.17 绍兴一中模拟赛 解题报告
  17. [ssh]permissions are too open密钥权限过大错误
  18. 牛蛙店老板的营销案例,开业3个月就还清80万贷款,就这还没完!
  19. Unix系统下修改密码
  20. 派克有铁芯无铁芯直线电机区别及应用

热门文章

  1. python input函数赋值法_大佬们 我是刚开始学python的小白 遇到这种赋值方式 实在不懂这个a+b是赋值给谁的 求解...
  2. Tomcat9 无法启动组件[Connector[AJP/1.3-8009]]
  3. 圆柱属于能滚动的物体吗_一户多宅属于违建吗?怎么能拥有多一点的宅基地
  4. 深入理解计算机系统学后感,深入理解计算机系统(读书笔记)
  5. 异构图-GTN(Graph Transformer Networks)
  6. java高并发(二)并发与高并发基本概念
  7. Python自动化办公之Excel拆分并自动发邮件
  8. 如何把薪资谈高一倍?请看大厂offer拿到手软的ML大神自述
  9. springboot 两个src_springboot application.properties 写多个配置文件怎么写
  10. 13-爬虫之js加密,解密,混淆,逆向破解思路