Cod sursa(job #778624)

Utilizator t.g.g.tt.g.g.t t.g.g.t Data 15 august 2012 11:27:29
Problema Secv Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.09 kb
var a,b:array[0..5000]of longint; i,j,n,k,k1:integer; t:boolean;
procedure quicksort (ins,sf:integer);
var i,s:integer; pivot,k:longint;
begin
  i:=ins; s:=sf; pivot:=a[(i+s) div 2];
  repeat
    while pivot>a[i] do inc(i);
    while pivot<a[s] do dec(s);
    if i<=s then begin k:=a[i]; a[i]:=a[s]; a[s]:=k; inc(i); dec(s) end;
  until i>s;
if (s>ins) then quicksort(ins,s);
if (i<sf)then quicksort(i,sf);
end;
begin
assign(input,'secv.in'); reset(input);
read(n);
for i:=1 to n do begin read(a[i]); b[i]:=a[i];end;
quicksort(1,n); t:=true; a[0]:=-1;k:=1;
for i:=1 to n do
  begin
    if a[i-1]<>a[i] then begin
      for j:=k to n do
        begin
          if b[j]=a[i] then
            if b[k]<>a[i] then
              begin
                k:=j;
              end;
              if j=n then
                if b[k]<>a[i] then
                  t:=false
        end;
                if i=1 then k1:=k
                end;
    end;
assign(output,'secv.out'); rewrite(output);
if t then if n=0 then writeln('0') else writeln(k-k1+1) else writeln('-1');
close(output);
end.