Problem 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

题意:给出 n 个点的坐标,以及一个数 k,这 n 个点之间的距离定义为彼此的曼哈顿距离,现在要将这些点连接起来,使得连接的权值最小,输出第 k 大的边

思路:本质是曼哈顿距离最小生成树,求出生成树后输出第 k 大的边即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 10000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;struct BIT{//树状数组int arr[N];int Id[N];void init(){memset(arr,INF,sizeof(arr));memset(Id,-1,sizeof(Id));}int lowbit(int k){return k&(-k);}void update(int pos,int val,int id){while(pos>0){if(arr[pos]>val){arr[pos]=val;Id[pos]=id;}pos-=lowbit(pos);}}int read(int pos,int m){int minval=INF;int ans=-1;while(pos<=m){if(minval>arr[pos]){minval=arr[pos];ans=Id[pos];}pos+=lowbit(pos);}return ans;}
}B;
struct POS{//区域int x,y;int id;bool operator<(const POS &rhs) const{if(x!=rhs.x)return x<rhs.x;return y<rhs.y;}
}pos[N];
struct Edge{int x,y;int dis;bool operator<(const Edge &rhs)const {return dis<rhs.dis;}
}edge[N<<2],resEdge[N<<2];
int edgeTot,resEdgeTot;
int father[N];
void build(int n){//在R1区域中建边sort(pos,pos+n);int a[N],b[N];for(int i=0;i<n;i++){a[i]=pos[i].y-pos[i].x;b[i]=pos[i].y-pos[i].x;}//离散化sort(b,b+n);int num=unique(b,b+n)-b;B.init();for(int i=n-1;i>=0;i--){int poss=lower_bound(b,b+num,a[i])-b+1;int ans=B.read(poss,num);if(ans!=-1){//建边edge[edgeTot].x=pos[i].id;edge[edgeTot].y=pos[ans].id;edge[edgeTot].dis=abs(pos[i].x-pos[ans].x)+abs(pos[i].y-pos[ans].y);//曼哈顿距离edgeTot++;}B.update(poss,pos[i].x+pos[i].y,i);}
}
void manhattan(int n,int k) {for(int dir=0;dir<4;dir++){//左侧四个区域if(dir==1||dir==3){//变换区域for(int i=0;i<n;i++)swap(pos[i].x,pos[i].y);}else if(dir==2){//变换区域for(int i=0;i<n;i++)pos[i].x=-pos[i].x;}build(n);//建边}
}
int Find(int x) {return father[x]==x?x:Find(father[x]);
}
void kruskal(int n,int k){resEdgeTot=0;for(int i=0;i<=n;i++)father[i]=i;sort(edge,edge+edgeTot);int cnt=n-k;for(int i=0;i<edgeTot;i++) {int x=edge[i].x,y=edge[i].y;int fx=Find(x),fy=Find(y);if(fx!=fy) {cnt--;father[fx]=fy;resEdge[resEdgeTot].x=x;resEdge[resEdgeTot].y=y;resEdge[resEdgeTot].dis=edge[i].dis;resEdgeTot++;}}}
int main(){int n,k;scanf("%d%d",&n,&k);edgeTot=0;resEdgeTot=0;for(int i=0;i<n;i++){scanf("%d%d",&pos[i].x,&pos[i].y);pos[i].id=i;}manhattan(n,k);kruskal(n,k);sort(resEdge,resEdge+resEdgeTot);printf("%d\n",resEdge[resEdgeTot-k].dis);return 0;
}

Object Clustering(POJ-3214)相关推荐

  1. POJ 3241 Object Clustering(Manhattan MST)

    题目链接:http://poj.org/problem?id=3241 Description We have N (N ≤ 10000) objects, and wish to classify ...

  2. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  3. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  4. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  5. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

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

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

  7. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

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

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

  9. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  10. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

最新文章

  1. 线性表ArrayList和LinkedList源码详解。
  2. LINUX不能ping域名, 能ping ip, 添加DNS解析
  3. linux下如何查询jdk的安装路径
  4. 用汇编的眼光看c++(之模板函数)
  5. ajax简单实例代码,分享Ajax创建简单实例代码
  6. 8皇后问题--回溯法 (循环递归)
  7. linux 硬连接 跨分区,Linux硬盘分区和软硬链接
  8. 用C++编写的小游戏源代码
  9. 表格操作系列——字段名与字段别名的获取
  10. triggered传递参数
  11. word自动添加题注,带章节编号并根据章节重新开始编号
  12. 知识图谱简介(一)——相关概念
  13. English in December
  14. Intel无线网卡linux,Gentoo 安装之intel无线网卡篇
  15. vivo手机删除自带程序方法
  16. webgl径向模糊实现体积光
  17. java around_关于Aop切面中的@Before @Around等操作顺序的说明
  18. 案例3-1 基于控制台的购书系统
  19. 如何登录锐捷设备(网关篇)
  20. 7 series FPGAs Transceiver Wizard IP核使用和测试

热门文章

  1. 未来已来!医院数字化转型为“看病难”画上“休止符”
  2. 300张小抄表搞定机器学习知识点:学习根本停不下来!
  3. 你H第一次做的视频,在B站播放量过万了~
  4. 做程序员10年了,复制粘贴是我最牛的技能,直到我看到了这几个公众号
  5. 史海峰:构建产业互联网金融系统的正确姿势
  6. 开源资讯- Jeecg 在线聊天MQ插件发布
  7. 职场 | 工作五年之后,对技术和业务的思考
  8. 在数学空间中,物理分辨率可能失去了意义(behind the paper)
  9. DBN【深度置信网络】【受限玻尔兹曼机深层】详解
  10. python脚本判断一个数是否为素数的几种方法