1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #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]; int xy[10005];
void update(int index,int minn,int 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+=1000; int e=-1,c=INF; 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() { sort(point+1,point+1+n); memset(id,-1,sizeof(id)); fill(xy,xy+10005,INF); 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() { 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; } build_edge();
for(int i=1;i<=n;i++) swap(point[i].x,point[i].y); build_edge();
for(int i=1;i<=n;i++) point[i].x=-point[i].x; build_edge();
for(int i=1;i<=n;i++) swap(point[i].x,point[i].y); build_edge(); kruskal(); return 0; }
|