POJ 3241 Object Clustering mo team algorithm

Source: Internet
Author: User


Section n-k big Manhattan Distance, MO team algorithm bare title


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

Description

We haveN(N≤10000) objects, and wish to classify them in several groups by judgement of their resemblance. To simply the model with each object have 2 indexesaandb(a,b≤500). The resemblance of objectIand objectJis defined byDij= |AI-AJ| + |Bi-BJ|, and then we sayIIsDijResemble toJ. Now we want to find the minimum value ofX, so, we can classify theNObjects intoK(K< N) groups, and in each group, one object are at the mostXResemble to another object in the same group, i.e, for every objectI, ifIIs isn't the only member of the group and then there exists one objectJ(I≠J) in the same group that satisfiesDijX

Input

The first line contains the integers N and K. The following N lines each contain, 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 December 20 Saturday 00:27 00 seconds file Name :P oj3241.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&LT;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);p t[i].x=x; PT [I].y=y; Pt[i].id=i;}printf ("%d\n", Solve ());} return 0;}




POJ 3241 Object Clustering mo team algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.