Cod sursa(job #772630)

Utilizator hungntnktpHungntnktp hungntnktp Data 30 iulie 2012 13:10:36
Problema Secventa Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.65 kb
CONST
        tfi     ='secventa.in';
        tfo     ='secventa.out';
        nmax    =500000;
TYPE
        arr     =array [1..nmax] of longint;
VAR
        fi,fo   :text;
        n,k,top,max,d,c:longint;
        a,b,st  :arr;
 (********************************************************************)
Procedure nhap;
      Var
        i       :longint;
      Begin
        assign(fi,tfi);reset(fi);
          read(fi,n,k);
          for i:=1 to n do read(fi,a[i]);
        close(fi);
      End;
 (*********************************************************************)
Procedure push(x:longint);
      Begin
        inc(top);
        st[top]:=x;
      End;
 (**********************************************************************)
Procedure lam;
      Var
        i,t,dem :longint;
      Begin
        push(1);
        t:=1;
        dem:=0;
        for i:=1 to n do
          begin
            if i-st[t]>=k then inc(t);
            while (top>=t) and (a[i]<=a[st[top]]) do dec(top);
            push(i);
            if i>=k then
              begin
                inc(dem);
                b[dem]:=st[t];
              end;
          end;
        for i:=1 to dem do if a[b[i]]>max then
          begin
            max:=a[b[i]];
            d:=i;
            c:=i+k-1;
          end;
      End;
 (************************************************************************)
Procedure inkq;
      Begin
        assign(fo,tfo);rewrite(fo);
          write(fo,d,' ',c,' ',max);
        close(fo);
      End;
 (************************************************************************)
BEGIN
        nhap;
        lam;
        inkq;
END.