POJ 3241 Object Clustering 莫隊演算法,pojclustering

來源:互聯網
上載者:User

POJ 3241 Object Clustering 莫隊演算法,pojclustering


第n-k大曼哈頓距離,莫隊演算法裸題


Object Clustering
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 1584   Accepted: 366

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 21 22 32 23 44 33 1

Sample Output

2

Source

POJ Monthly--2007.08.05, Li, Haoyuan

[Submit]   [Go Back]   [Status]   [Discuss]




/* ***********************************************Author        :CKbossCreated Time  :2014年12月20日 星期六 00時27分00秒File Name     :POJ3241.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=22000;const int INF=0x3f3f3f3f;int n,k;struct Edge{int u,v,w;}edge[maxn<<2];int tot;bool cmp_E(Edge a,Edge b){return a.w<b.w;}struct Point{int x,y,id;}pt[maxn];bool cmp_P(Point a,Point b){if(a.x!=b.x) return a.x<b.x;return a.y<b.y;}int lowbit(int x) { return x&(-x); }int C[4000],D[4000];void update(int id,int pos,int val){pos+=1000;for(int i=pos;i;i-=lowbit(i)){if(C[i]>val){C[i]=val; D[i]=id;}}}void query(int id,int pos,int val){pos+=1000;int Fr=-1,ret=INF;for(int i=pos;i<4000;i+=lowbit(i)){if(ret>C[i]){ret=C[i]; Fr=D[i];}}if(Fr!=-1) edge[tot++]=(Edge){id,Fr,ret-val};}void gao(){memset(C,63,sizeof(C));sort(pt,pt+n,cmp_P);for(int i=n-1;i>=0;i--){int pos=pt[i].y-pt[i].x;int sum=pt[i].y+pt[i].x;int id=pt[i].id;query(id,pos,sum);update(id,pos,sum);}}int fa[maxn];int find(int x){if(x==fa[x]) return x;return fa[x]=find(fa[x]);}bool merge(int a,int b){a=find(a); b=find(b);if(a==b) return false;fa[a]=b;return true;}int solve(){if(n==k) return 0;tot=0;gao();for(int i=0;i<n;i++)swap(pt[i].x,pt[i].y);gao();for(int i=0;i<n;i++)pt[i].y=-pt[i].y;gao();for(int i=0;i<n;i++)swap(pt[i].x,pt[i].y);gao();for(int i=0;i<n;i++) fa[i]=i;sort(edge,edge+tot,cmp_E);for(int i=0;i<tot;i++){if(merge(edge[i].u,edge[i].v)==true){n--; if(n==k) return edge[i].w;}}return -1;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    while(scanf("%d%d",&n,&k)!=EOF){for(int i=0;i<n;i++){int x,y;scanf("%d%d",&x,&y);pt[i].x=x; pt[i].y=y; pt[i].id=i;}printf("%d\n",solve());}    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.