Cod sursa(job #51382)

Utilizator ProtomanAndrei Purice Protoman Data 11 aprilie 2007 20:24:21
Problema Secventa Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.91 kb
var a,q:array[1..500000] of longint; i,ic,d,r,poz,n,k:longint; f1,f2:text;
Begin
        assign(f1,'secventa.in');
        reset(f1);
        assign(f2,'secventa.out');
        rewrite(f2);
        read(f1,n,k);
        for i:=1 to n do read(f1,a[i]);
        r:=-maxlongint;ic:=1;d:=0;
        for i:=1 to k-1 do begin
                if d>=ic then while a[i]<=a[q[d]] do begin dec(d); if d=ic then break; end;
                inc(d);
                q[d]:=i;
        end;
        for i:=k to n do
        begin
                if d>=ic then while a[i]<=a[q[d]] do begin dec(d); if d=ic then break; end;
                inc(d);
                q[d]:=i;
                if d>=ic then while q[ic]<i-k+1 do begin inc(ic); if d=ic then break; end;
                if a[q[ic]]>r then begin r:=a[q[ic]]; poz:=i; end;
        end;
        write(f2,poz-k+1,' ',poz,' ',r);
        close(f1);
        close(f2);
End.