ac1264.pas

来自「这是在网络上搜集到的在东京大学的ACM上面解决的一些题目的源码」· PAS 代码 · 共 24 行

PAS
24
字号
{$Q-,R-}
program tju1264;
const
  maxn=1000000;
var
  d:array[1..maxn]of word;
  bq,sq:array[1..maxn]of longint;
  n,k,i,bf,br,sf,sr,head,ans,m:longint;
begin
  repeat
    read(n,k);
    bf:=1;br:=0;sf:=1;sr:=0;head:=0;ans:=0;
    for i:=1 to n do begin
      read(d[i]);
      while (br>=bf) and (d[i]>=d[bq[br]]) do dec(br);inc(br);bq[br]:=i;
      while (sr>=sf) and (d[i]<=d[sq[sr]]) do dec(sr);inc(sr);sq[sr]:=i;
      if d[bq[bf]]-d[i]>k then begin repeat inc(bf);until d[bq[bf]]-d[i]<=k;head:=bq[bf-1];end;
      if d[i]-d[sq[sf]]>k then begin repeat inc(sf);until d[i]-d[sq[sf]]<=k;head:=sq[sf-1];end;
      if i-head>ans then ans:=i-head;
    end;
    writeln(ans);
  until seekeof;
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?