Cod sursa(job #222604)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 23 noiembrie 2008 20:33:25
Problema Secventa Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.23 kb
var f,g:text;
    v,q,p:array[0..500010]of longint;
    n,k,i,st,dr,max,p2,j,x:longint;
    c:array[1..1500000]of char;
    s:char;

begin
assign(f,'secventa.in');
assign(g,'secventa.out');
reset(f);
rewrite(g);
readln(f,n,k);
read(f,c);
i:=1;
n:=0;
x:=0;
s:='+';
while (c[i] in ['0'..'9','-',' '])do
  begin
    if (c[i]=' ')then
      begin
        if (s='-')then
          begin
            if (x>0)then x:=-x;
          end else
            if (x<0)then x:=-x;
        inc(n);
        v[n]:=x;
        x:=0;
        s:='+';
      end else
    if (c[i]='-')then s:='-' else
    if (c[i] in ['0'..'9'])then
      begin
        if (i=1)or(c[i-1]='-')then
          x:=x*10+ord(c[i])-ord('0')else
          x:=x*10+ord(c[i])-ord('0');
      end;
    inc(i);
  end;
inc(n);
v[n]:=x;
st:=1;
dr:=1;
q[1]:=v[1];
p[1]:=1;
max:=-30001;
for i:=2 to n do
  begin
    while (v[i]<=q[dr])and(dr>=st)do
      dec(dr);
    inc(dr);
    q[dr]:=v[i];
    p[dr]:=i;
    if (i>=k)then
      begin
        while (p[st]<i-k+1)do inc(st);
        if (q[st]>max)then
          begin
            max:=q[st];
            p2:=i;
          end;
      end;
  end;
write(g,p2-k+1,' ',p2,' ',max);
close(f);
close(g);
end.