POJ 3241 曼哈顿距离最小生成树 Object Clustering
先上几个资料:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, m, k; struct Point
{
int x, y, id;
bool operator < (const Point& p) const {
return x < p.x || (x == p.x && y < p.y);
}
}; Point p[maxn]; int dist(Point a, Point b) { return abs(a.x-b.x) + abs(a.y-b.y); } struct Node
{
int min_val, pos;
void init() { min_val = INF; pos = -; }
}; inline int lowbit(int x) { return x & (-x); } Node BIT[maxn]; struct Edge
{
int u, v, d;
Edge(int u, int v, int d):u(u), v(v), d(d) {}
bool operator < (const Edge& e) const { return d < e.d; }
}; vector<Edge> edges; int pa[maxn];
int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); } int a[maxn], b[maxn]; void update(int x, int val, int pos) {
while(x) {
if(val < BIT[x].min_val) {
BIT[x].min_val = val;
BIT[x].pos = pos;
}
x -= lowbit(x);
}
} int query(int x) {
int val = INF, pos = -;
while(x <= m) {
if(BIT[x].min_val < val) {
val = BIT[x].min_val;
pos = BIT[x].pos;
}
x += lowbit(x);
}
return pos;
} int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%d%d", &n, &k) == && n)
{
edges.clear();
for(int i = ; i < n; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
} //Select edges
for(int dir = ; dir < ; dir++) {
//Coordinate Transformation
if(dir == || dir == ) for(int i = ; i < n; i++) swap(p[i].x, p[i].y);
else if(dir == ) for(int i = ; i < n; i++) p[i].x = -p[i].x; sort(p, p + n);
for(int i = ; i < n; i++) a[i] = b[i] = p[i].y - p[i].x;
sort(b, b + n);
m = unique(b, b + n) - b;
for(int i = ; i <= m; i++) BIT[i].init(); for(int i = n - ; i >= ; i--) {
int pos = lower_bound(b, b + m, a[i]) - b + ;
int q = query(pos);
if(q != -) edges.push_back(Edge(p[i].id, p[q].id, dist(p[i], p[q])));
update(pos, p[i].x + p[i].y, i);
}
} //Kruskal
sort(edges.begin(), edges.end());
int cnt = n - k, ans;
for(int i = ; i < n; i++) pa[i] = i;
for(int i = ; i < edges.size(); i++) {
int u = edges[i].u, v = edges[i].v;
int pu = findset(u), pv = findset(v);
if(pu != pv) {
if(--cnt == ) { ans = edges[i].d; break; }
pa[pu] = pv;
}
} printf("%d\n", ans);
} return ;
}
代码君
POJ 3241 曼哈顿距离最小生成树 Object Clustering的更多相关文章
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- 51nod 1213 二维曼哈顿距离最小生成树
1213 二维曼哈顿距离最小生成树 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间 ...
- 曼哈顿距离最小生成树 codechef Dragonstone
曼哈顿距离最小生成树 codechef Dragonstone 首先,对于每一个点来说有用的边只有它向它通过 x=0,y=0,y=x,y=-x 切出来的八个平面的最近点. 证明 我不会 反正当结论记住 ...
- POJ 3241Object Clustering曼哈顿距离最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into several ...
- [51nod1213]二维曼哈顿距离最小生成树
二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离).求这N个点所组成的完全图 ...
- LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)
题目大意: 曼哈顿最小距离生成树 算法讨论: 同上. 这回的模板真的准了. #include <iostream> #include <cstring> #include &l ...
- 【Poj3241】Object Clustering
Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...
- 曼哈顿距离MST
https://www.cnblogs.com/xzxl/p/7237246.html 讲的不错 /* 曼哈顿距离最小生成树 poj 3241 Object Clustering 按照上面的假设我们先 ...
- 【poj3241】 Object Clustering
http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...
随机推荐
- ABAP数据类型
数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1 Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...
- 用Jersey为Android客户端开发Restful Web Service
平时在做Android客户端的时候经常要与服务器之间通信,客户端通过服务端提供的接口获取数据,然后再展示在客户端的界面上,作为Android开发者,我们平时更多的是关注客户端的开发,而对服务端开发的关 ...
- UVA 140 Brandwidth 带宽 (dfs回溯)
看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...
- python_90_hashlib模块
#用于加密相关的操作,3.x里代替了2.x中的md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib ...
- 变量和数据类型&运算符
变量和数据类型&运算符 变量 变量的作用:用来存储数据 变量命名的规范:字(字符串)下(_下划线)美($)人(¥) 数 (可以包括数字)骆驼 有意义(可以以字母,下划线,美元符号,人民币符号开 ...
- 《毛毛虫组》【Alpha】Scrum meeting 4
第二天 日期:2019/6/17 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: 货物入库管理模块设计: (1)对数据库表--tb_OutStore进行修改并完善: (2)学习trig_ ...
- STM32F042开发板学习实践
之前有粗略地学习过stm32F103的理论知识,但仅GPIO的配置.时钟配置就让人望而却步.现在手头有Nucleo STM32F042的开发板,而且ST已经推出STM32CubeMx图形化的工具软件, ...
- 2019.05.26 周日--《阿里巴巴 Java 开发手册》精华摘要
一.写在开头 Java作为一个编程界最流行的语言之一,有着很强的生命力.代码的编写规范也是不容忽视的,今天,我就把自己阅读的国内的互联网巨头阿里巴巴的<阿里巴巴 Java 开发手册>一些精 ...
- ES6 -- 模板字符串(反单引号)
1)直接使用变量 // before var str = 'test'; console.log(str + "123"); // now var str = 'test'; co ...
- salt 模板
http://www.mamicode.com/info-detail-2297406.html