Description
To find the longest sequence, the difference between the maximum minimum value is not greater than \ (K\).
Sol
Monotone queue.
A queue is directly on the line.
Code
/************************************************************** problem:2096 User:beiyu language:c++ Result : Accepted time:4024 ms memory:71604 kb****************************************************************/#include &L T;bits/stdc++.h>using namespace Std; #define DEBUG (a) cout<< #a << "=" <<a<< "typedef long LONG ll;const int N = 3e6+50; LL N,k,ans; LL A[n]; LL q[2][n],h[2],t[2],l; inline ll in (ll X=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x; }int Main () {k=in (), N=in (); for (int i=1;i<=n;i++) a[i]=in (); Ans=1; q[0][h[0]=t[0]=1]=1,q[1][h[1]=t[1]=1]=1,l=1; for (int i=2;i<=n;i++) {while (h[0]<=t[0] && a[i]<a[q[0][t[0]]) t[0]--; Q[0][++t[0]]=i; cout<<i<< ":" <<endl;//for (int j=h[0];j<=t[0];j++) cout<<q[0][j]<<endl;while (H[1]<=t[1] && a[i]>a[q[1][t[1]]) t[1]--; Q[1][++t[1]]=i; for (int j=h[1];j<=t[1];j++) cout<<q[1][j]<<endl; for (; a[q[1][h[1]]]-a[q[0][h[0]]]>k;l++) {if (q[0][h[0]]==l) h[0]++; if (q[1][h[1]]==l) h[1]++; } ans=max (ans,i-l+1);//debug (L), debug (ans) <<endl;//cout<< "--------------------" <<endl ; }cout<<ans<<endl; return 0;}
Bzoj 2096: [poi2010]pilots