Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

NOIP program 03

Program NOIP_program_03;
var
  n,s,i :longint;
  a,t :array[0..100005] of longint;
 procedure mergesort(l,r : longint);
 var
   mid,p,i,j :longint;
begin
  if (l=r) then exit;
  mid:= (l+r) div 2;
  p:=1;
  i:=1;
  j:=mid + 1;
  mergesort(l, mid);
  mergesort(mid +1, r);
  while (i<= mid) and (j<= r) do
  begin
    if (a[j] <a[i]) then
    begin
      s:= s+ mid-i+1;
      t[p]:= a[j];
      p:= p+1;
      j:= j+1;
      end else
      begin
        t[p] := a[i];
        p:= p+1;
        i:= i+1;
      end;
    end;
    while (i<= mid) do
    begin
      t[p]:= a[i];
      p:= p+1;
      i:= i+1;
    end;
    while (j<= r) do
    begin
      t[p]:= a[j];
      p:= p+1;
      j:= j+1;
    end;
    for i:= l to r do
      a[i]:= t[i];
    end;
    begin
      read(n);
      for i:= 1 to n do
        read(a[i]);
      writeln;
      writeln;
      mergesort(1,n);
      writeln(s);
end.

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.