Cod sursa(job #181673)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 18 aprilie 2008 18:40:46
Problema Secv Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.29 kb
var a,poz,aux,min,subs : array[0..5010] of longint;
    use : array[0..5010] of boolean;
    minf,i,j,tb,n,nr,con : longint;
    f,g : text;
procedure sort(l,r : integer);
var i,j,x,y,aux : longint;
begin
     i:=l; j:=r; x:=a[(l+r)div 2];
     repeat
           while (a[i]<x) do inc(i);
           while (x<a[j]) do dec(j);
           if i<=j then
           begin
                aux:=a[i];a[i]:=a[j];a[j]:=aux;
                aux:=poz[i];poz[i]:=poz[j];poz[j]:=aux;
                inc(i); dec(j);
           end;
     until i>j;
     if l<j then sort(l,j);
     if i<r then sort(i,r);
end;

begin
assign(f,'secv.in');reset(f);
assign(g,'secv.out');rewrite(g);
read(f,n);
for i:=1 to n do poz[i]:=i;
for i:=1 to n do
  read(f,a[i]);
aux:=a;
sort(1,n);
for i:=1 to n do
begin
  if a[i]<>a[i-1] then
  begin
    inc(nr);
    subs[nr]:=a[i];
  end;
end;
a:=aux;
for i:=1 to 5000 do min[i]:=maxlongint;
for i:=1 to n-nr+1 do
begin
  for j:=1 to nr do use[j]:=false;
  con:=0; tb:=1;
    for j:=i to n do
    begin
      if a[j]=subs[tb] then inc(tb);
      if tb=nr+1 then
      begin
        min[i]:=j-i+1;
        break;
      end;
    end;
end;

minf:=maxlongint;
for i:=1 to n do
  if min[i]<minf then minf:=min[i];
writeln(g,minf);
close(g);
end.