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. Linux服务器架设篇,DNS服务器(二),cache-only DNS服务器的搭建

    一.理论基础 什么是cache-only服务器?即不具备自己正反解Zone的能力,仅进行缓存或转发的DNS服务器.其实它也称不上是DNS服务器.但是也是一个必备的知识点. 这种服务器只有缓存搜索结果的 ...

  2. 使用 Chrome 插件 Vimium 打造黑客浏览器

    之前一直用 cVim,与 Vimium 功能类似,但是之后不在更新了,故转战到 Vimium. 简介 官网:http://vimium.github.io/ Vimium 是 Google Chrom ...

  3. stand up meeting 12/22/2015 && 用户体验收录

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  完善页面切换,尝试子页面设计    4  完善页面切换和子页面 ...

  4. K - Two Contests

    题目连接:https://atcoder.jp/contests/agc040/tasks/agc040_b 大佬题解:https://blog.csdn.net/duanghaha/article/ ...

  5. A - Chat Group Gym-101775A

    题目连接:https://codeforces.com/gym/101775/problem/A 题解:就是累加组合数 但是直接由K累加到N肯定会TLE ,所以我们不妨判断不能组成group的情况,即 ...

  6. ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题

    引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --& ...

  7. mybatis源码配置文件解析之二:解析settings标签

    在前边的博客中分析了mybatis解析properties标签,<mybatis源码配置文件解析之一:解析properties标签>.下面来看解析settings标签的过程. 一.概述 在 ...

  8. web form常用控件

    表单元素一共12个分三大类 文本类<input type="text" />             文本框<input type="password& ...

  9. Redis安装部署(一主二从三哨兵)

    需求:根据当前客户的生产环境,模拟安装部署Redis的测试环境,方便后续的功能测试. 1.准备工作 2.安装编译Redis 3.Redis运行环境配置 4.Redis启动和关闭 1.准备工作 Redi ...

  10. [PHP][thinkphp5] 学习二:路由、配置调用、常量定义调用

    路由: 其实TP5就是一个集多家框架所长而成的,感觉失去了自己的特色!路由这块呢类似于laravel框架!废话不说直接上码! 路由配置,类似laravel,就在route.php文件里配置路由(文件所 ...