var
n,i:longint;
ans:int64;
a,temp:array[1..100000] of longint;
procedure merge(l,m,r:longint);
var
i,j,k:longint;
begin
i:=l;
j:=m+1;
for k:=l to r do
begin
if(i<=m) and ((j>r)or(a[i]<=a[j])) then
begin
temp[k]:=a[i];
inc(i);
end
else
begin
temp[k]:=a[j];
inc(j);
inc(ans,m-i+1);{統計此區間內的逆序對數,若右區間有輸出則將ans加上左區間剩下的個數}
end;
end;
for k:=l to r do a[k]:=temp[k];
end;
procedure sort(l,r:longint);
var
m:longint;
begin
m:=(l+r) shr 1;
if l<>r then
begin
sort(l,m);
sort(m+1,r);
merge(l,m,r);
end;
end;
begin
ans:=0;
read(n);
for i:=1 to n do
read(a[i]);
sort(1,n);
writeln(ans);
end.
逆序對數{nlogn,歸排}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.