Http://codeforces.com/contest/602/problem/B
Two ways of doing this problem,
Sliding window + segment tree query interval max:
#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intInf= (1<< in);structsegtree{structNode {intL,r; intMin,max; }; Node T[MAXN<<2]; voidPUSH_UP (intRT) {T[rt]. Max=max (t[rt<<1]. max,t[rt<<1|1]. MAX); T[RT]. Min=min (t[rt<<1]. min,t[rt<<1|1]. Min); } voidBuildint*a,intLintRintRT) {T[RT].L=l; T[rt].r=R; if(l==R) {T[rt]. Max=T[RT]. min=A[l]; return; } intM= (l+r) >>1; Build (A,l,m,rt<<1); Build (A,m+1,r,rt<<1|1); PUSH_UP (RT); } intQuerymax (intLintRintRT) { if(L<=T[RT].L&&T[RT].R<=R)returnT[rt]. Max; intM= (T[RT].L+T[RT].R) >>1; intres=-INF; if(l<=m) Res=max (Res,querymax (l,r,rt<<1)); if(r>m) Res=max (Res,querymax (l,r,rt<<1|1)); returnRes; } intQuerymin (intLintRintRT) { if(L<=T[RT].L&&T[RT].R<=R)returnT[rt]. Min; intM= (T[RT].L+T[RT].R) >>1; intres=INF; if(l<=m) Res=min (Res,querymin (l,r,rt<<1)); if(r>m) Res=min (Res,querymin (l,r,rt<<1|1)); returnRes; }}; Segtree Sgt;intA[maxn],n;intMain () {//freopen ("In.txt", "R", stdin); while(cin>>N) {REP (i,1, N) scanf ("%d",&A[i]); Sgt.build (A,1N1); intPre=1; intans=1; REP (i,1, N) { while(Sgt.querymax (Pre,i,1)-sgt.querymin (Pre,i,1) >1) pre++; Ans=max (ans,i-pre+1); } cout<<ans<<Endl; } return 0;}
View Code
Sliding window + Monotone queue maintenance sliding window maximum:
#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1<< in;d eque<int>q1,q2;intN,A[MAXN];intMain () {Freopen ("In.txt","R", stdin); while(cin>>N) {REP (i,1, N) scanf ("%d",&A[i]); intPre=1, ans=1; while(!q1.empty ()) Q1.pop_back (); while(!q2.empty ()) Q2.pop_back (); REP (i,1, N) { while(!q1.empty () &&q1.back () <A[i]) q1.pop_back (); Q1.push_back (A[i]); while(!q2.empty () &&q2.back () >A[i]) q2.pop_back (); Q2.push_back (A[i]); while(Q1.front ()-q2.front () >1){ if(a[pre]==Q1.front ()) Q1.pop_front (); if(a[pre]==Q2.front ()) Q2.pop_front (); Pre++; } ans=max (ans,i-pre+1); } cout<<ans<<Endl; } return 0;}
View Code
First monotone Queue = =
codeforces#333 div2 B. Approximating a Constant Range