Cod sursa(job #771992)

Utilizator t.g.g.tt.g.g.t t.g.g.t Data 27 iulie 2012 20:10:19
Problema Secventa Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.99 kb
var a:array[1..500001]of integer; b:array[1..500001]of longint; i,n,k,p,u,m:longint;
buf:array[1..1 shl 19]of char; s:ansistring; se:boolean;
begin
assign(input,'secventa.in'); reset(input); settextbuf(input,buf);
readln(n,k);a[n+1]:=-31000;
readln(s);i:=1;p:=1;
while i<=length(s) do
  begin
    if s[i]='-' then begin se:=true; i:=i+1; end else se:=false;
    while ((i)<=length(s)) and (s[i]<>' ') do
      begin
        a[p]:=a[p]*10+(ord(s[i])-48);
        i:=i+1;
      end;
      if (i<=length(s)) and (s[i]=' ') then inc(i);
    if se then a[p]:=-a[p];
    p:=p+1;
  end;
p:=1; u:=0; m:=n+1;
for i:=1 to n do
  begin
    while (p<=u) and (a[i]<a[b[u]]) do u:=u-1;
    u:=u+1;
    b[u]:=i;
    if (b[p]=i-k) then p:=p+1;
    if i>=k then if a[m]<a[b[p]] then m:=b[p];
  end;
i:=m;
if i>1 then while (i>1) and (a[i-1]>=a[m]) do i:=i-1;
p:=m-i+1; if p<k then p:=i+k-1 else p:=m;
assign(output,'secventa.out'); rewrite(output);
writeln(i,' ',p,' ',a[m]);
close(output);
end.