Object Clustering

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.

Sample Input

6 2

1 2

2 3

2 2

3 4

4 3

3 1

Sample Output

2

Source

POJ Monthly–2007.08.05, Li, Haoyuan

题解

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi=acos(-1.0);
const double E=2.718281828459;
const ll mod=1e8+7;
const int INF=0x3f3f3f3f; int n,k; struct node{
int x;
int y;
int id;
bool friend operator<(node a,node b){
return a.x==b.x?a.y<b.y:a.x<b.x;
///保证树状数组更新和查询时不会遗漏
}
}point[10005]; struct edge{
int s;
int e;
int c;
bool friend operator<(edge a,edge b){
return a.c<b.c;
}
}load[40000];
int sign=0;
int p[10005];
int find(int x){
return p[x]==x?x:p[x]=find(p[x]);
} void kruskal(){
for(int i=1;i<=n;i++)
p[i]=i;
sort(load+1,load+1+sign);
int cnt=0;
for(int i=1;i<=sign;i++){
int x=find(load[i].s);
int y=find(load[i].e);
if(x!=y){
cnt++;
p[x]=y;
if(cnt==n-k){
printf("%d\n",load[i].c);
return ;
}
}
}
} int id[10005]; ///y-x为索引的编号
int xy[10005]; ///y-x为索引 x+y的最小值 void update(int index,int minn,int s) ///index:y-x minn:x+y s:编号
{
index+=1000;
for(int i=index;i>=1;i-=(i&(-i))){
if(xy[i]>minn){
xy[i]=minn;
id[i]=s;
}
}
} void query(int index,int minn,int s) ///index:y-x minn:x+y s:编号
{
index+=1000;
int e=-1,c=INF;
///现在以编号s为原点,查询y-x>=index的点中x+y的最小值
for(int i=index;i<10000;i+=(i&(-i))){
if(xy[i]<c){
e=id[i];
c=xy[i];
}
}
if(e!=-1)
load[++sign]=edge{s,e,c-minn};
} void build_edge()
{
/// 以(xi,yi)为原点,对于第1区域内的点(x,y)满足条件
/// x>=xi,y-x>=yi-xi,(x+y)最小
sort(point+1,point+1+n);
memset(id,-1,sizeof(id));
fill(xy,xy+10005,INF);
///按照x升序
///保证后面查询时,x都比当前的x大
for(int i=n;i>=1;i--){
int index=point[i].y-point[i].x;
int minn=point[i].x+point[i].y;
query(index,minn,point[i].id);
update(index,minn,point[i].id);
}
} int main() ///第K大边
{
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d %d",&point[i].x,&point[i].y);
point[i].id=i;
}
///1象限建边
build_edge(); ///2象限建边
for(int i=1;i<=n;i++)
swap(point[i].x,point[i].y);
build_edge(); ///3象限建边
for(int i=1;i<=n;i++)
point[i].x=-point[i].x;
build_edge(); ///4象限建边
for(int i=1;i<=n;i++)
swap(point[i].x,point[i].y);
build_edge();
kruskal();
return 0;
}

POJ 3241Object Clustering曼哈顿距离最小生成树的更多相关文章

  1. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  2. 51nod 1213 二维曼哈顿距离最小生成树

    1213 二维曼哈顿距离最小生成树 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间 ...

  3. 曼哈顿距离最小生成树 codechef Dragonstone

    曼哈顿距离最小生成树 codechef Dragonstone 首先,对于每一个点来说有用的边只有它向它通过 x=0,y=0,y=x,y=-x 切出来的八个平面的最近点. 证明 我不会 反正当结论记住 ...

  4. [51nod1213]二维曼哈顿距离最小生成树

    二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离).求这N个点所组成的完全图 ...

  5. POJ 3241 曼哈顿距离最小生成树 Object Clustering

    先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...

  6. LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)

    题目大意: 曼哈顿最小距离生成树 算法讨论: 同上. 这回的模板真的准了. #include <iostream> #include <cstring> #include &l ...

  7. 曼哈顿距离MST

    https://www.cnblogs.com/xzxl/p/7237246.html 讲的不错 /* 曼哈顿距离最小生成树 poj 3241 Object Clustering 按照上面的假设我们先 ...

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

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

  9. poj 2926:Requirements(最远曼哈顿距离,入门题)

    Requirements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3908   Accepted: 1318 Desc ...

随机推荐

  1. Docker-准备Docker环境

    1. 前言 要学习Docker,首先要搭建出虚拟机和docker环境.我的笔记本电脑是windows7系统,用VirtualBox创建虚拟机.虚拟机操作系统为CentOS7.5,Docker版本为18 ...

  2. L10机器

    机器翻译和数据集 机器翻译(MT):将一段文本从一种语言自动翻译为另一种语言,用神经网络解决这个问题通常称为神经机器翻译(NMT). 主要特征:输出是单词序列而不是单个单词. 输出序列的长度可能与源序 ...

  3. 装机摸鱼日记01--DDR3AMD专用内存+QHQF(6400T)试水

    前些日子在某鱼入手了两条AMD专用内存,宏想的DDR3-1600MHz-8G内存和一块高贵阿苏斯Z170-P-D3主板,然后某宝600多买了一颗QHQF(当然也可以玩QHQJ,更便宜,估计三百多),准 ...

  4. Jmeter--Mysql数据库压力测试

    前提环境要求:首先下载合适的数据库驱动 传送门:https://mvnrepository.com/artifact/mysql/mysql-connector-java 将下载好的驱动放到Jmete ...

  5. [php] 简单的实现一个错误接管类

    自己弄的一个错误接管类: <?php //---------------------------------- // Leephp 错误接管类 // 2017-07-06 // Pengchon ...

  6. [git] github上传项目(使用git)、删除项目、添加协作者

    来源:http://www.cnblogs.com/sakurayeah/p/5800424.html (怕链接失败,所以直接就就复制过来啦,感谢作者) 一.注册github账号 github网址ht ...

  7. 美的PDF转换成Word转换器完全免费

    下载地址:百度网盘提取码:02ap 安装破解步骤:先安装主程序,末尾是full结尾的,安装完成后不要打开软件,然后接着安装破解补丁,即可破解成功! 需要的老铁们直接拿去用吧,亲测好用!有配套的功能强大 ...

  8. 图解AVL树

    1:AVL树简介 二叉搜索树在一般情况下其搜索的时间复杂度为O(logn),但某些特殊情况下会退化为链表,导致树的高度变大且搜索的时间复杂度变为O(n),发挥不出树这种数据结构的优势,因此平衡二叉树便 ...

  9. 使用RNN对文本进行分类实践电影评论

    本教程在IMDB大型影评数据集 上训练一个循环神经网络进行情感分类. from __future__ import absolute_import, division, print_function, ...

  10. react: typescript custom hooks useAsyncTable

    define basic data: const SET_QUERY = "SET_QUERY"; const TOGGLE_LOADING = "TOGGLE_LOAD ...