# bzoj 1604 奶牛的邻居

## Description

1．两只奶牛的曼哈顿距离不超过C(1≤C≤10^9)，即lXi - xil+IYi - Yil≤C.
2．两只奶牛有共同的邻居．即，存在一只奶牛k，使i与k，j与k均同属一个群．
给出奶牛们的位置，请计算草原上有多少个牛群，以及最大的牛群里有多少奶牛

## Input

第1行输入N和C，之后N行每行输入一只奶牛的坐标．

## 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.

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define R register int
4 #define rep(i,a,b) for(R i=a;i<=b;i++)
5 #define Rep(i,a,b) for(R i=a;i>=b;i--)
6 #define ms(i,a)    memset(a,i,sizeof(a))
8     x=0; char c=0;
9   while (!isdigit(c)) c=getchar();
10   while (isdigit(c)) x=x*10+(c^48),c=getchar();
11 }
12 int const N=100003;
13 struct node{
14   int vy,id;
15   bool operator < (const node & rhs) const{
16     if(vy!=rhs.vy) return vy<rhs.vy;
17     else return id<rhs.id;
18   }
19 };
20 struct cow{
21   int x,y;
22   bool operator< (const cow &rhs) const{
23     return x<rhs.x;
24   }
25 }a[N];
26 int n,c,f[N],sz[N],vis[N],ans;
27 set<node> s;
28 set<node> :: iterator p;
29 int gf(int x){return x==f[x]?  x:f[x]=gf(f[x]);}
30 void un(int x,int y){
31   int fx=gf(x),fy=gf(y);
32   if(fx==fy) return ;
33   f[fx]=fy; sz[fy]+=sz[fx];ans--;
34 }
35 int main(){
37   rep(i,1,n){
39     a[i].x=x+y;a[i].y=x-y;
40   }
41   sort(a+1,a+n+1);
42   rep(i,1,n) f[i]=i,sz[i]=1;
43   int k=1;
44   rep(i,1,n){
45     while (a[i].x-a[k].x>c){
46       node t=(node){a[k].y,k}; k++;  s.erase(t);
47     }
48     node t;t.vy=a[i].y;t.id=i;
49     p=s.lower_bound(t);
50     if(p!=s.end()){
51       if(p->vy-a[i].y<=c) un(i,p->id);
52       if(p!=s.begin()){
53         --p; if(a[i].y-p->vy<=c)  un(i,p->id);
54       }
55     }else if(s.size()){
56       p=--s.end();
57       if(a[i].y-p->vy<=c) un(i,p->id);
58     }
59     s.insert(t);
60   }
61   int maxs=0;
62   rep(i,1,n) {
63     int fx=gf(i);
64     if(vis[fx]) continue;
65     vis[fx]=1;
66     maxs=max(maxs,sz[fx]);
67   }
68   cout<<ans<<" "<<maxs<<endl;
69   return 0;
70 }
View Code

posted @ 2019-01-01 10:43  zjxxcn  阅读(166)  评论(0编辑  收藏  举报