Cod sursa(job #1089324)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 21 ianuarie 2014 17:13:47
Problema Secv Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.97 kb
var n,m,r,i,j,k,x:longint;
    v,t,c:array[1..5000] of longint;
begin
  assign(input,'secv.in');reset(input);
  assign(output,'secv.out');rewrite(output);
  read(n);
  r:=1000000000;
  for i:=1 to n do
    begin
      read(v[i]);
      t[i]:=v[i];
    end;
  for i:=1 to n do
      for j:=i+1 to n do
          if t[i]>t[j] then
           begin
             x:=t[i];
             t[i]:=t[j];
             t[j]:=x;
           end;
  c[1]:=t[1];
  m:=1;
  for i:=2 to n do
    if t[i]<>t[i-1] then
       begin
         m:=m+1;
         c[m]:=t[i];
       end;
  for i:=n downto 1 do
    begin
      if v[i]=c[m] then
        begin
          k:=i;
          for j:=m-1 downto 1 do
            begin
              while (k>0)and(v[k]<>c[j]) do dec(k);
              if k<=0 then break;
            end;
          if (k>0)and(r>i-k+1) then r:=i-k+1;
        end;
    end;

  if r=1000000000 then writeln(-1) else writeln(r);
  close(output);
end.