For the right and left end l,r of the legal interval, it is clear that the leftmost L increases with r without decreasing.
Maintain a monotone queue for the maximum and minimum values, and if it is illegal, move the L and out of the team.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 Const intmaxn=3000233;8 intmx[maxn],mn[maxn],id1[maxn],id2[maxn],l1,r1,l2,r2;9 inti,j,k,n,m,x,ans,k,l;Ten One intRaCharRx; AInlineintRead () { -Rx=getchar (), ra=0; - while(rx<'0'|| Rx>'9') rx=GetChar (); the while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; - } -InlineintGetdelta (intl) { - while(l1<r1&&id1[l1]<l) l1++; + while(l2<r2&&id2[l2]<l) l2++; - returnmx[l1]-MN[L2]; + } AInlineintMaxintAintb) {returnA>b?a:b;} at intMain () { -K=read (), n=read (); -L=l1=l2=1, r1=r2=0; - for(i=1; i<=n;i++){ -x=read (); - while(l1<=r1&&mx[r1]<=x) r1--; in while(l2<=r2&&mn[r2]>=x) r2--; -mx[++r1]=mn[++r2]=x,id1[r1]=id2[r2]=i; to while(L<i&&getdelta (L) >k) l++; + //printf ("%d--%d\n", l,i); -Ans=max (ans,i-l+1); the } *printf"%d\n", ans); $ }Panax Notoginseng
View Code
[bzoj2096] [Poi2010] Pilots