Descriptionpeople who know cows know that cows like to be in droves. Observe John's N (1≤n≤100000) cows, and you will find that they have formed several "groups". Every cow has a unique position when grazing. Xi,yi (L≤xi,yi≤[1..10^9];xi,yi∈ Integer. When one of the following two conditions is met, two cows I and J belong to the same group:1. The distance between the two cows in Manhattan is no more than C (1≤c≤10^9), or lxi-xil+iyi-yil≤c.2. The two cows have a common neighbour. That is, there is a cow k, so that I and k,j and K belong to a group. given the location of the cows, please calculate how many herds there are on the prairie and how many cows are in the largest herd .Input

Line 1, enter N and C, and then enter the coordinates of a cow for each row of n rows.

Output

Only one row, first output the number of herds, and then output the maximum number of cows in the herd, separated by a space.

Sample Input4 2

1 1

3 3

2 2

10 10

* Line 1: A single line with a space-separated integers:the

Number of cow neighborhoods and the size of the largest cow

Neighborhood.

Sample Output 2 3

OUTPUT DETAILS:

There is 2 neighborhoods, one formed by the first three cows and

The other being the last cow. The largest neighborhood therefore

Has size 3.

—————————————————————————————The first thing we do is transform each point into

**(sx= (x+y), sy= (x-y))** so the two-point Manhattan distance is

**Max (|sx1–sx2|, |sy1–sy2|) =max (X1-x2+y1-y2, x1-x2-y1+y2)** This can be classified as four types of proof1

**x1>x2 &&y1>y2** so

**sx1-sx2** is the normal representation of the distance in Manhattan and

**Sx1-sx2>sy1-sy2** 2

** X1<x2&&y1<y2** because it is absolute, the form is the same (IBID.)3

**x1>x2&&y1<y2** so

**|x1-x2|+|y1-y2| = X1-x2+y2-y1** is sy1-sy2 also easy to get sy1-sy2>sx1-sx2 3

**x1>x2&&y2>y1** is the same as the absolute value of inverted just a bit (IBID.)So after we put the click SX Sort the difference of the SX that maintains a queue's head coordinates and the coordinates of the current point cannot exceed Cbecause more than c he must not be with the current point Manhattan distance less than Cso if more than we want to delete (by the way in the balance tree the point is deleted)then we maintain a sy balance tree to find out the current point sy in the balance tree of the precursor to see if you can mergeif it can be merged in one and look up the setthe last sweep will give you an answer.As to why this can be combined, because even if the other points are below the precursor or under the following and they can merge with the current pointthen they must merge before and after the precursor or before, so this kind of greed is possible.

#include <cstdio>#include<cstring>#include<algorithm>#include<Set>#include<queue>using namespacestd;Const intm=150007;intRead () {intans=0, f=1, c=GetChar (); while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {ans=ans*Ten+ (C-'0'); C=GetChar ();} returnans*F;}intn,c;intf[m],h[m],ans,mx;intFindintx) { while(F[X]!=X) x=f[x]=f[f[x]];returnx;}structnode{intX,y,id; BOOL operator< (Constnode& k)Const{returnx<k.x;}} E[M];structpos{intY,id; BOOL operator< (Constpos& k)Const{returny!=k.y?y<k.y:id<k.id;}}; Std::queue<int>Q;std::multiset<pos>Tr;std::multiset<pos>:: iterator it;intMain () {intx, y; N=read (); C=read (); for(intI=1; i<=n;i++) {x=read (); y=read (); F[i]=i; E[i].x=x+y,e[i].y=x-y; E[i].id=i; } sort (E+1, e+1+N); for(intI=1; i<=n;i++){ while(!q.empty () &&e[i].x-e[q.front ()].x>c) { intnow=Q.front (); Q.pop (); Tr.erase (Tr.find ((POS) {e[now].y,e[now].id})); } q.push (i); It=Tr.insert (POS) {e[i].y,e[i].id}); if(it!=Tr.begin ()) { --it; if(e[i].y-(it->y) <=c) { intP=find (E[i].id), Q=find (it->ID); F[Q]=p; } ++it; } ++it; if(it!=Tr.end ()) { if(it->y-e[i].y<=c) { intP=find (E[i].id), Q=find (it->ID); F[Q]=p; } } } for(intI=1; i<=n;i++){ intx=find (e[i].id); if(!h[x]) ans++; H[X]++; mx=Max (mx,h[x]); }printf ("%d%d\n", ANS,MX); return 0;}

View Code

Bzoj 1604: [Usaco2008 open]cow neighborhoods cows ' neighbors--sort + greedy +set