Cod sursa(job #82145)

Utilizator gurneySachelarie Bogdan gurney Data 5 septembrie 2007 19:30:25
Problema Barman Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.26 kb
program barman;
  const
    fin='barman.in';
    fout='barman.out';
    nmax=600;
  var
    aa,a,at,obt:array[1..nmax] of longint;
    min:longint;
    oc:array[1..nmax] of boolean;
    i,j,x,y,n,k,next,vnext:longint;
begin
  assign(input,fin);
    reset(input);
    readln(n);
    for i:=1 to n do
      read(a[i]);
  close(input);
  assign(output,fout);
    rewrite(output);
    aa:=a;
    for i:=1 to n-1 do
      for j:=i+1 to n do
        if aa[i]>aa[j] then
          begin
            x:=aa[i];aa[i]:=aa[j];aa[j]:=x;
          end;
    at:=a;
    min:=maxlongint;
    for i:=1 to n do
      begin
        for j:=i to n do
          obt[j-i+1]:=aa[j];
        for j:=1 to i-1 do
          obt[n-i+1+j]:=aa[j];
        a:=at;
        x:=0;
        fillchar(oc,sizeof(oc),false);
        for j:=1 to n do
          if a[j]<>obt[j] then
            begin
              inc(x,20);
              for k:=1 to n do
                if ((oc[k]=false)and(obt[k]=a[j]))and(a[j]<>a[k])then
                  begin
                    oc[k]:=true;
                    inc(x,abs(k-j));
                    break;
                  end;
            end;
        if x<min then
          min:=x;
      end;
    writeln(min);
  close(output);
end.