Cod sursa(job #71086)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 9 iulie 2007 10:54:40
Problema Secventa Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.25 kb
program sir_;

const v=100000;

type sir=array[1..v]of longint;

var a,minim,maxim:sir;
    s,t,pp,uu,p,u,n,x,y,z,l,lung,inceput,sf:longint;


procedure citire;
var i:longint;
begin
assign(input,'sir.in');
reset(input);
read(n,x,y,z);
for i:=1 to n do
    read(a[i]);
close(input);
end;

procedure min(i:longint);
begin
inc(u);
if u>v then
   u:=1;
minim[u]:=i;
while (a[minim[u]]<a[minim[u-1]]) and (u<>p) do
      begin
      if u-1<1 then
         begin
         minim[v]:=minim[u];
         u:=v;
         end else begin
                  minim[u-1]:=minim[u];
                  dec(u);
                  end;
      end;
if (minim[u]-minim[p]+1>y) then
   inc(p);
end;

procedure max(i:longint);
begin
inc(uu);
if uu>v then
   uu:=1;
maxim[uu]:=i;
while (a[maxim[uu]]>a[maxim[uu-1]]) and (uu<>pp) do
      begin
      if uu-1<1 then
         begin
         maxim[v]:=maxim[u];
         uu:=v;
         end else begin
                  maxim[uu-1]:=maxim[uu];
                  dec(uu);
                  end;
      end;
if (maxim[uu]-maxim[pp]+1>y) then
   inc(pp);
end;

procedure lungime;
begin
if a[maxim[pp]]-a[minim[p]]<=z then
   begin
   l:=abs(minim[p]-maxim[pp])+1;
   if minim[p]<maxim[pp] then
      begin
      s:=minim[p];
      t:=maxim[pp];
      end else begin
               t:=minim[p];
               s:=maxim[pp];
               end;
   while (l+1<=y) and (s-1>=1) and (a[s-1]>=a[minim[p]]) and (a[s-1]<=a[maxim[pp]]) do
         begin
         dec(s);
         inc(l);
         end;
   while (l+1<=y) and (t+1<=n) and (a[t+1]>=a[minim[p]]) and (a[t+1]<=a[maxim[p]]) do
         begin
         inc(t);
         inc(l);
         end;
   if (l>lung) and (l>=x) and (l<=y) then
      begin
      lung:=l;
      inceput:=s;
      sf:=t;
      end;
   end;
end;

procedure rezolvare;
var i:longint;
begin
minim[1]:=1;
p:=1;
u:=1;
maxim[1]:=1;
pp:=1;
uu:=1;
for i:=2 to n do
    begin
    min(i);
    max(i);
    lungime;
    end;
end;

procedure scriere;
begin
assign(output,'sir.out');
rewrite(output);
if inceput=-1 then
   write(inceput)
   else write(lung,' ',inceput,' ',sf);
close(output);
end;

begin
citire;
inceput:=-1;
rezolvare;
scriere;
end.