题意:二维平面上有n个点,要求选取至多k个点,使得每个点到离它最近的点的距离最大值最小。
解法:二分距离,用重复覆盖判断选出的点数。
但这个题直接求可以选出的最少点数会t,所以要在剪枝的时候加一点特殊的技巧。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf (1e18)
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define ll long long
#define uLL unsigned LL
using namespace std;
const int maxn = 60 *60 * 2;
int k;
struct sad
{
int L[maxn],R[maxn],U[maxn],D[maxn];
int Row[maxn],Col[maxn];
int h[maxn] , s[maxn];
int si,ans;
void init(int m)
{
for(int i=0;i<=m;i++)
{
U[i] = D[i] = i;
L[i] = i-1;
R[i] = i+1;
s[i] = 0;
}
L[0] = m;
R[m] = 0;
clr(h,-1);
si=m;
ans=2132;
}
void link(int row,int col){
si++;
Col[si] = col;
Row[si] = row;
s[col]++;
U[si] = U[col];
D[si] = col;
D[U[col]] = si;
U[col] = si;
if(h[row]!=-1)
{
R[si] = h[row];
L[si] = L[h[row]];
R[L[si]] = si;
L[R[si]] = si;
}else
h[row] = L[si] = R[si] = si;
}
void Remove(int c)
{
for(int i=D[c];i!=c;i=D[i]){
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int c)
{
for(int i=U[c];i!=c;i=U[i]){
L[R[i]] = R[L[i]] = i;
}
}
int vis[maxn];
int H()
{
int ret=0;
for(int i=R[0];i!=0;i=R[i])
vis[i] = 0;
for(int i=R[0];i!=0;i=R[i]) if(!vis[i]){
ret++;
vis[i] = 1;
for(int j=D[i];j!=i;j=D[j])
for(int k=R[j];k!=j;k=R[k])
vis[Col[k]] = 1;
}
return ret;
}
void dfs(int cnt)
{
if(cnt+H() > k) return ;
if(R[0]==0){
ans = min(ans,cnt);
return ;
}
int c=R[0];
for(int i=R[0];i!=0;i=R[i])
if(s[c]>s[i]) c=i;
for(int i=D[c];i-c;i=D[i]){
Remove(i);
for(int j=R[i];j-i;j=R[j])
Remove(j);
dfs(cnt+1);
if( ans <= k ) return ;
for(int j=L[i];j!=i;j=L[j])
Resume(j);
Resume(i);
}
return ;
}
}dlx;
int x[maxn],y[maxn];
int n;
ll cal(int i,int j){
return (ll)abs(x[i]-x[j]) + (ll)abs(y[i]-y[j]);
}
vector<pair<ll,pair<int,int> > >Q;
bool cal(ll D){
dlx.init(n);
for(int i=0;i<Q.size();i++)
{
if( Q[i].first > D ) break;
dlx.link( Q[i].second.first , Q[i].second.second );
}
dlx.dfs(0);
return dlx.ans <= k;
}
int main(){
// freopen("input.txt","r",stdin);
int T,CASE=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
ll l=0,r=0,ans;
Q.clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
r = max(r,cal(i,j));
Q.push_back( make_pair( cal(i,j) , make_pair(i,j) ) );
}
sort(Q.begin(),Q.end());
ans = r;
while( l <= r )
{
ll mid=l+r>>1;
if( cal(mid) )
{
ans = min(ans,mid);
r = mid - 1;
}else l = mid + 1;
}
printf("Case #%d: %I64d\n",++CASE,ans);
}
return 0;
}