bzoj1604[usaco2008 open]cow neighborhoods cows ' neighbors
Test instructions
n Cattle, cattle group when and only two cattle in Manhattan distance ≤c or the presence of a third cow makes two cows and its Manhattan distance ≤c, the largest group and number of groups. n≤100000
Exercises
Oh, good god. First turn the Manhattan distance to Chebyshev distance, (x, Y) to (x+y,x-y). Then sorted by x-coordinate, with two pointer maintenance so that the x-coordinate difference ≤c, while the new y-coordinate inserted into the set, each time in the set to find out with the current Y difference value not more than the maximum Y, the two points are combined into a set, and check set maintenance.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <Set>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 1001007 #defineINF 0X3FFFFFFF8 using namespacestd;9 TenInlineintRead () { One CharCh=getchar ();intf=1, x=0; A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; the } - structp{intx, y;}; P PS[MAXN]; - BOOLCMP (P a,p b) {returna.x==b.x?a.y<b.y:a.x<b.x;} - structdata{ + intid,v; - BOOL operator< (ConstData &a)Const{returnv==a.v?id<a.id:v<A.V;} + }; A Set<data> s;intN,C,L,FA[MAXN],CNT[MAXN]; at intFindintx) {returnx==fa[x]?x:fa[x]=find (Fa[x]);} - voidMergeint_x,int_y) {intX=find (_x), Y=find (_y);if(x!=y) fa[y]=x;} - intMain () { -N=read (), C=read (); Inc (I,1, N) {intA=read (), B=read (); Ps[i]= (P) {a+b,a-b};} Sort (ps+1, ps+1+n,cmp); -S.clear (); S.insert (data) {0, INF}); S.insert (data) {0,-INF}); -L=1; S.insert (data) {1, ps[1].Y}); Inc (I,1, N) fa[i]=i; inInc (I,2, N) { - while(ps[i].x-ps[l].x>c) S.erase (s.find (data) {L,PS[L].Y}), l++; to Set<data>::iterator X=s.lower_bound (data) {0, ps[i].y}), y=x; y--; + if(x->v-ps[i].y<=c) merge (I,x->id);if(ps[i].y-y->v<=c) Merge (i,y->ID); - S.insert (data) {i,ps[i].y}); the } *Inc (I,1, n) cnt[find (i)]++;intans=0; Inc (I,1, N)if(Cnt[i]) ans++; printf"%d", ans); $ans=0; Inc (I,1, N) Ans=max (Ans,cnt[i]); printf"%d", ans);return 0;Panax Notoginseng}
20160809
bzoj1604[usaco2008 open]cow Neighborhoods cows ' neighbors *