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 dijX

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

又是一道曼哈顿距离最小生成树。。。
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 100005
#define inf 1061109567
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
struct Point{
int x,y,d,id;
}point[maxn],tmp[maxn];
bool cmp1(Point a,Point b){
if (a.x!=b.x) return a.x>b.x;
return a.y>b.y;
}
int calc(Point a,Point b){return abs(a.x-b.x)+abs(a.y-b.y);}
int n,m,k,ans;
struct DATA{
int val,pos;
void init(){val=inf,pos=-;}
void update(DATA b){if (val>b.val) val=b.val,pos=b.pos;}
};
int d[maxn],cntd;
struct bit{
#define lowbit(x) ((x)&(-(x)))
DATA node[maxn];
void init(){for (int i=;i<=cntd;i++) node[i].init();}
void insert(int x,DATA p){for (int i=cntd-x+;i<=cntd;i+=lowbit(i)) node[i].update(p);}
int query(int x){
DATA ans; ans.init();
for (int i=cntd-x+;i;i-=lowbit(i)) ans.update(node[i]);
return ans.pos;
}
}T;
struct Edge{
int u,v,c;
}edge[maxn<<];
bool cmp2(Edge a,Edge b){return a.c<b.c;}
void prepare(){
for (int i=;i<=n;i++) d[i]=point[i].d=point[i].y-point[i].x;
sort(d+,d+n+),cntd=unique(d+,d+n+)-d-;
for (int i=;i<=n;i++) point[i].d=lower_bound(d+,d+cntd+,point[i].d)-d;
sort(point+,point+n+,cmp1),T.init();
for (int i=;i<=n;i++){
int u=point[i].id,v=T.query(point[i].d);
if (v!=-) edge[++m]=(Edge){u,v,calc(tmp[u],tmp[v])};
T.insert(point[i].d,(DATA){point[i].x+point[i].y,u});
}
}
int fa[maxn];
int find(int x){return x==fa[x]?fa[x]:fa[x]=find(fa[x]);}
int main(){
read(n),read(k);
for (int i=;i<=n;i++) read(point[i].x),read(point[i].y),point[i].id=i;
for (int i=;i<=n;i++) tmp[i]=point[i]; prepare();
for (int i=;i<=n;i++) point[i].x=tmp[i].y,point[i].y=tmp[i].x,point[i].id=i; prepare();
for (int i=;i<=n;i++) point[i].x=-tmp[i].y,point[i].y=tmp[i].x,point[i].id=i; prepare();
for (int i=;i<=n;i++) point[i].x=tmp[i].x,point[i].y=-tmp[i].y,point[i].id=i; prepare();
sort(edge+,edge+m+,cmp2);
for (int i=;i<=n;i++) fa[i]=i;
for (int i=,cnt=n;i<=m&&cnt>k;i++) if (find(edge[i].u)!=find(edge[i].v))
cnt--,ans=max(ans,edge[i].c),fa[find(edge[i].u)]=find(edge[i].v);
printf("%d\n",ans);
return ;
}

 

最新文章

  1. 《JavaScript高级程序设计》 - 读书笔记 - 第4章 变量、作用域和内存问题
  2. paip. 提升性能---hibernate的缓存使用 总结
  3. Servlet部分细节介绍
  4. JavaScript String(字符串)对象 实例
  5. -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究
  6. 层叠样式表(CSS)
  7. Unix/Linux环境C编程新手教程(12) openSUSECCPP以及Linux内核驱动开发环境搭建
  8. Session,有没有必要使用它?
  9. 让C#、VB.NET实现复杂的二进制操作
  10. Request和Response的格式
  11. centos7操作记录
  12. 遇到短信轰炸,别人换ip调你的短信接口怎么办
  13. Request、Response
  14. Kotlin入门(11)江湖绝技之特殊函数
  15. postgre
  16. Generator yield语法和 co模块
  17. Using Dtrace OEL 6.X
  18. js 去掉html标签
  19. sql,将一天所有记录按小时划分
  20. ESXi服务器遇到 IPMI_SI_DRV 的解决, 感谢原作者 以及今天 解决问题.

热门文章

  1. C#执行带参数的Oracle存储过程
  2. Centos6.5 nginx+nginx-rtmp配置流媒体服务器
  3. 坚持c++,真正掌握c++(2)
  4. UML进行Linux内核调试
  5. [转] 一个资深iOS开发者对于React Native的看法
  6. Android中多线程下载列表的封装实现(含进度反馈)
  7. 最近的两个小项目,2:Python webapp的docker镜像
  8. Java NIO 学习笔记
  9. weex-toolkit打包
  10. Android开发---支付宝功能接口(支付功能)(转载!)