Cod sursa(job #31257)

Utilizator CezarMocanCezar Mocan CezarMocan Data 15 martie 2007 18:36:44
Problema Secventa Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.93 kb
type vector=array[0..10000]of longint;
var v,x:vector;
    i,j,n,nx,ls,ld,min:longint;

begin
  assign(input,'secv.in');reset(input);
  assign(output,'secv.out');rewrite(output);
  readln(n);
  read(v[1]);x[1]:=v[1]; nx:=1;
  //construiesc vectorul de elemente distincte in ordine crescatoare
  for i:=2 to n do begin
     read(v[i]);
     j:=1;while (x[j]<v[i])and(j<=nx) do inc(j);
     if (v[i]<x[j])or(j>nx) then begin
       for ls:=nx downto j do x[ls+1]:=x[ls];
       x[j]:=v[i];
       inc(nx);
     end;
  end;
  //caut secventele ce contin subsir crescator pe x
  min:=MaxLongInt;
  for i:=1 to n do
    if v[i]=x[1] then begin
      ls:=i; ld:=i;
      for j:=2 to nx do begin
        while (v[ld]<>x[j])and(ld<=n) do inc(ld);
        if ld>n then break;
      end;
     if (ld<=n)and(min>ld-ls+1) then min:=ld-ls+1;
    end;
  if min>n then write(-1) else write(min);
  close(input);close(output);
end.