Cod sursa(job #849064)

Utilizator daokiemDaonguyenDuong daokiem Data 6 ianuarie 2013 03:49:52
Problema Secventa Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.75 kb
CONST tfi='secventa.in';
      tfo='secventa.out';
VAR fo,fi:text;
    N,k,l,r,l_:longint;
    res,r1,r2:longint;
    a,st:array[1..500000] of longint;
procedure nhap;
  var i:longint;
  begin
       assign(fi,tfi);reset(fi);
       assign(fo,tfo);rewrite(fo);
         read(fi,n,k);
         for i:=1 to n do read(fi,a[i]);
       close(fi);
  end;
procedure push(i:longint);
  begin
        while (l<=r) and (a[i]<=a[st[r]]) do Dec(r);
        Inc(r);
        st[r]:=i;
  end;
procedure Xuli;
  var i,j:longint;
  begin
       l:=1;r:=0;res:=-MaxLongint;
       r1:=MaxLongint;r2:=r1;
       For i:=1 to n do
          begin
                push(i);
                l_:=l;
                while (l_<=r) and (st[l_]<=i-k) do
                  begin
                        Inc(l_);
                        if a[st[l_]]>res then
                          begin
                               res:=a[st[l_]];
                               r1:=st[l_];
                               r2:=i;
                          end
                        else
                          if a[st[l_]]=res then
                            begin
                                  if st[l_]<r1 then
                                    begin
                                         r1:=st[l_];
                                         r2:=i
                                    end
                                  else if st[l_]=r1 then
                                    begin
                                          if i<r2 then r2:=i;
                                    end;
                            end;
                  end;
          end;
        write(fo,r1,' ',r2,' ',res);
        close(fo);
  end;
BEGIN
      nhap;
      Xuli;
END.