Cod sursa(job #136375)

Utilizator you_reheroMihai Gojinetchi you_rehero Data 15 februarie 2008 15:10:05
Problema Secv Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.61 kb
var fi,fo:text;
    n,i,ct,poz,min,dist,k,j:longint;
    nr,x:array[1..5000]of longint;
    ap:array[1..1500000000]of byte;
    ok:byte;
function part(st,dr:integer):integer;
var i,j,aux,s:integer;
begin
  i:=st; j:=dr; s:=-1;
  while i<j do
    begin
      if nr[i]>nr[j] then
        begin
          aux:=nr[i];
          nr[i]:=nr[j];
          nr[j]:=aux;
          s:=-s;
        end;
      if s=1 then inc(i)
             else dec(j);
    end;
  part:=i;
end;
procedure qsort(st,dr:integer);
var p:integer;
begin
  if st<dr then
    begin
      p:=part(st,dr);
      qsort(st,p-1);
      qsort(p+1,dr);
    end;
end;
begin
  assign(fi,'secv.in'); reset(fi);
  assign(fo,'secv.out'); rewrite(fo);
  read(fi,n); ct:=0;
  for i:=1 to n do
    begin
      read(fi,x[i]);
      if ap[x[i]]=0 then
        begin
          inc(ct);
          nr[ct]:=x[i];
          ap[x[i]]:=1;
        end;
    end;
  qsort(1,ct);
  min:=maxint;
  for i:=1 to n do
    if x[i]=nr[1] then
      begin
        dist:=0;
        poz:=i;
        ok:=1;
        for j:=2 to ct do
         if ok=1 then
          begin
             for k:=poz+1 to n do
               if x[k]=nr[j] then
                 begin
                   poz:=k;
                   ok:=1;
                   break;
                 end
               else ok:=0;
          end;
         if ok=1 then
           begin
             dist:=poz-i+1;
             if dist<min then min:=dist;
           end;
        end;
  {if min<>maxint then writeln(fo,min)
                 else} writeln(fo,-1);
  close(fi);
  close(fo);
end.