Cod sursa(job #66261)

Utilizator ScrazyRobert Szasz Scrazy Data 17 iunie 2007 11:14:57
Problema Secv Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.49 kb
const MaxN=5002;
type vektor=array[1..MaxN] of longint;
var

  fin,fout:text;
  a,c:vektor;
  n,i,j,m,z:integer;

  min,minh:longint;
  ok:boolean;


procedure QSort(var a:vektor;Lo, Hi: longint);

procedure Sort(l, r:longint);
var
  i, j, x, y: longint;
begin
  i := l; j := r; x := a[random(r-l)+l];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

begin
  Sort(Lo,Hi);
end;


begin

  assign(fin,'secv.in');reset(fin);
  assign(fout,'secv.out');rewrite(fout);

  readln(fin,n);

  min:=maxlongint;
  m:=0;


  for i:=1 to n do begin
    read(fin,a[i]);

    ok:=true;
    if min>a[i] then min:=a[i];

    for j:=1 to m do
      if a[i]=c[j] then ok:=false;

    if ok then begin
      m:=m+1;
      c[m]:=a[i];
    end;

  end;

  qsort(c,1,m);

  minh:=maxlongint;
  for i:=1 to n-m+1 do begin
      if min=a[i] then begin
        z:=i+1;
        j:=2;
        while (z<=n)and(j<=m) do begin

           if a[z]=c[j] then j:=j+1
                        else z:=z+1;
        end;

        if (j=m+1)and(z<=n) then begin
          if minh>z-i+1 then minh:=z-i+1;
        end;
      end;
  end;

  if (minh=maxlongint)or(minh<m) then writeln(fout,-1)
                     else writeln(fout,minh);


  close(fout);

end.