# 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离

（最近p站上不去要死了）

Description

1．两只奶牛的曼哈顿距离不超过C(1≤C≤10^9)，即lXi - xil+IYi - Yil≤C.
2．两只奶牛有共同的邻居．即，存在一只奶牛k，使i与k，j与k均同属一个群．

Input

Output

Sample Input
4 2
1 1
3 3
2 2
10 10
* Line 1: A single line with a two space-separated integers: the number of cow neighborhoods and the size of the largest cow neighborhood.
Sample Output
2 3
OUTPUT DETAILS:
There are 2 neighborhoods, one formed by the first three cows and the other being the last cow. The largest neighborhood therefore has size 3.

#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=100000+5;
const int oo=0x3f3f3f3f;

int n,c,fa[N],siz[N];
bool vis[N];
struct node{
int x,y;
int num;
}a[N];
set<node> S;

inline bool operator < (const node &a,const node &b){
if(a.y==b.y) return a.num<b.num;
return a.y<b.y;
}

bool cp(const node &a,const node &b){
return a.x==b.x&&a.y==b.y&&a.num==b.num;
}
bool cmp(const node &a,const node &b){
return a.x<b.x;
}
int getfa(int x){
if(fa[x]==x) return x;
return fa[x]=getfa(fa[x]);
}
void merge(int x,int y){
int fx=getfa(x),fy=getfa(y);
if(fx==fy) return ;
fa[fx]=fy;
}
int main(){
scanf("%d%d",&n,&c);
int xx,yy;
for(int i=1;i<=n;i++){
scanf("%d%d",&xx,&yy);
a[i].x=xx+yy,a[i].y=xx-yy;
a[i].num=i;
fa[i]=i;
}
sort(a+1,a+n+1,cmp);
node inf,_inf;
inf.x=_inf.x=inf.num=_inf.num=0,inf.y=oo,_inf.y=-oo;
S.insert(inf);
S.insert(_inf);
S.insert(a[1]);
node pre,sub;
int tmp=1;
for(int i=2;i<=n;i++){
while(a[i].x-a[tmp].x > c){
S.erase(a[tmp]);
tmp++;
}
S.insert(a[i]);
pre=*--S.find(a[i]);
sub=*++S.find(a[i]);
if(pre.y!=-oo){
if(a[i].y-pre.y<=c){
merge(a[i].num,pre.num);
}
}
if(sub.y!=oo){
if(sub.y-a[i].y<=c){
merge(a[i].num,sub.num);
}
}
}
int maxn=0,cnt=0;
for(int i=1;i<=n;i++){
int f=getfa(a[i].num);
siz[f]++;
}
for(int i=1;i<=n;i++){
if(siz[i]) cnt++;
maxn=max(maxn,siz[i]);
}
printf("%d %d\n",cnt,maxn);
return 0;
}
posted @ 2017-10-31 19:09  LinnBlanc  阅读(118)  评论(0编辑  收藏  举报