Maintain two monotonous queues, one storing the maximum value before the current vertex.
In addition, the minimum value before the current vertex is stored.
If the difference between the maximum value and the minimum value is greater than k, move backward from the front of the maximum and minimum values.
#include
#include
#include#include
#include
using namespace std;//#define INF ((1<<30)-1)#define INF 0xfffff#define maxn 220000#define LL long long#define MOD 1000000009struct list{ int val; int x;}p[maxn],q[maxn],pp;int main(){ int n,m,k,i,x; while(~scanf("%d%d%d",&n,&m,&k)) { int h1,h2,t1,t2; h1=h2=1; t1=t2=0; int last1,last2,ans; ans=last1=last2=0; for(i=1;i<=n;i++) { scanf("%d",&x); pp.x=i; pp.val=x; while(t1>=h1&&p[t1].val
=h2&&q[t2].val>x)t2--; q[++t2]=pp; while(p[h1].val-q[h2].val>k) { if(p[h1].x
=m) { ans=max(ans,i-max(last1,last2)); } } cout<