Cod sursa(job #115777)

Utilizator raduzerRadu Zernoveanu raduzer Data 16 decembrie 2007 22:35:01
Problema Grozavesti Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.38 kb
var a:array[1..310,1..310]of longint;
    b,c:array[1..310]of longint;
    n,i,j,rez,z:longint;

procedure Sort(l, r: longint);
var
  i, j, x, y: longint;
begin
  i := l; j := r; x := b[(l+r) DIV 2];
  repeat
    while b[i] < x do i := i + 1;
    while x < b[j] do j := j - 1;
    if i <= j then
    begin
      y := b[i]; b[i] := b[j]; b[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;

procedure bubble;
var i,sch,j:longint;
begin
     repeat
           sch:=0;
           for i:=1 to n do
           begin
                if b[i]<>a[i][i] then
                begin
                     for j:=1 to n do
                     begin
                          if (b[j]=a[i][i])and(a[j][j]<>a[i][i]) then
                          begin
                               rez:=rez+2;
                               z:=a[i][i];
                               a[i][i]:=a[j][j];
                               a[j][j]:=z;
                               sch:=1;
                          end;
                     end;
                end;
           end;
     until sch=0;
end;

procedure bubble2;
var i,sch,j:longint;
begin
     repeat
           sch:=0;
           for i:=1 to n do
           begin
                if b[i]<>c[i] then
                begin
                     for j:=1 to n do
                     begin
                          if (b[j]=c[i])and(c[j]<>c[i]) then
                          begin
                               writeln('C ',i,' ',j);
                               writeln('L ',i,' ',j);
                               z:=c[i];
                               c[i]:=c[j];
                               c[j]:=z;
                               sch:=1;
                          end;
                     end;
                end;
           end;
     until sch=0;
end;


begin
     assign(input,'grozavesti.in');
     reset(input);
     assign(output,'grozavesti.out');
     rewrite(output);
     readln(n);
     for i:=1 to n do
     begin
          for j:=1 to n do
          begin
               read(a[i][j]);
          end;
          readln;
     end;
     for i:=1 to n do
     begin
          b[i]:=a[i][i];
          c[i]:=a[i][i];
     end;
     sort(1,n);
     bubble;
     writeln(rez);
     bubble2;
close(output);
end.