hdu 3401 用两个单调队列 递减+递加
program p5;
type
rec=record
num:longint;data:longint;
end;
var
queue1,queue2:array[1..100005] of rec;
head1,tail1,head2,tail2:longint;
pos,n,m,k,i,j:Longint;
num:array[1..100005] of longint;
ans:longint;
tmp:rec;
begin
while not seekeof do begin{pascal 这里要seekeof,否则会出错}
readln(n,m,k);
for i:=1 to n do read(num[i]);
ans:=0;head1:=1;tail1:=0;
head2:=1;tail2:=0;pos:=1;
for i:=1 to n do begin
tmp.num:=i;tmp.data:=num[i];
while (tail1>=head1) and (tmp.data>queue1[tail1].data) do dec(tail1);
inc(tail1);queue1[tail1]:=tmp;
while (tail2>=head2) and (tmp.data<queue2[tail2].data) do dec(tail2);
inc(tail2);queue2[tail2]:=tmp;
while queue1[head1].data-queue2[head2].data>k do begin
if queue1[head1].num>queue2[head2].num then begin
pos:=queue2[head2].num+1;
inc(head2);
end
else begin
pos:=queue1[head1].num+1;inc(head1);
end;
end;
if (queue1[head1].data-queue2[head2].data<=k) and (queue1[head1].data-queue2[head2].data>=m) then
if i-pos+1>ans then ans:=i-pos+1;
end;
writeln(ans);
end;
end.