Cod sursa(job #184720)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 24 aprilie 2008 09:54:21
Problema Secventa Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.04 kb
var a,q:array[1..500000] of longint;
    n,k,i,st,dr,rez,poz:longint;
    f,g:text;

procedure citire;
 var x,p,poz:longint;
     ch:char;
 begin
  poz:=0;
  while not(eoln(f)) do begin
   poz:=poz+1;
   p:=1; x:=0; read(f,ch);
   if ch='-' then
    p:=-1
   else
    x:=ord(ch)-48;
   read(f,ch);
   while not(eoln(f)) and (ch<>' ') do begin
    x:=x*10+ord(ch)-48;
    read(f,ch);
   end;
   if ch in ['0'..'9'] then
    x:=x*10+ord(ch)-48;
   a[poz]:=x*p;
  end;
 end;

begin
 assign(f,'secventa.in'); reset(f);
 assign(g,'secventa.out'); rewrite(g);
 readln(f,n,k);
 citire;
 rez:=-2000000000;
 st:=1;
 dr:=0;
 for i:=1 to k-1 do begin
  while(dr>=st) and (a[i]<=a[q[dr]]) do
   dr:=dr-1;
  dr:=dr+1;
  q[dr]:=i;
 end;
 for i:=k to n do begin
  while(dr>=st) and (a[i]<=a[q[dr]]) do
   dr:=dr-1;
  dr:=dr+1;
  q[dr]:=i;
  while (st<=dr) and (q[st]<i-k+1) do
   inc(st);
  if a[q[st]]>rez then begin
   rez:=a[q[st]];
   poz:=i;
  end;
 end;
 writeln(g,poz-k+1,' ',poz,' ',rez);
 close(f); close(g);
end.