Олимпиадный тренинг

Задача 18780. Number of operations


Задача

Темы:
Number of operations
 
Given a sorting program (p141.pas). Need to know how many times
when sorting a specific array with this program
the operation of comparing two elements of the array is performed (line 25 of the program).
 
Input data
The number N is given first (1≤N≤100), and then N integers, modulo not exceeding 1000.
 
Output
Your program should print one number - how much
once in the process of sorting this array by the p141.pas program, the
will be executed
command to compare two array elements.
 
Sample input file
5
3 1 2 4 2
 
Sample Output
10

Program text p141.pas
const nmax=100;

var a:array[1..nmax] of integer;
    n:integer;
    i,j,g:integer;

    f1,f2:text;

begin
assign(f1,'input.txt');
reset(f1);
assign(f2,'output.txt');
rewrite(f2);
                                  {Reading input}
read(f1,n);
for i:=1 to n do read(f1,a[i]);
                                  {Sort array}

for i:=1 to n do begin {Select number for i-th place}

  g:=i; {We assume that the smallest number,
                                   which we met stands in place i}

  for j:=i+1 to n do {Iterate over all numbers from i+1 to the end of the array}
    if a[j]<a[g] then g:=j; {If you find a number that is less than
                                   than what is already found, remember it}

                                  {Swap the numbers on the i-th and
                                   in the g-th places }
                                  {If a[i]=x, a[g]=y, then after executing
                                   commands: }
  if i<>g then begin
    a[i]:=a[i]+a[g]; {a[i]=x+y, a[g]=y}
    a[g]:=a[i]-a[g]; {a[i]=x+y, a[g]=(x+y)-y=x}
    a[i]:=a[i]-a[g]; {a[i]=(x+y)-x=y}
                                  {That is, after that a[i]=y, a[g]=x
                                   exchange of values ​​occurred}
    end;

  end;

                                  {Display result}
for i:=1 to n do
  write(f2,a[i],' ');
close(f1);
close(f2);
end.