嗯。。前几天刚刚把它给写掉了。。没时间写文章。。现在把它给补掉把> <
好像没什么好说的。。直接看代码?
Code:
var a,b:array[0..500000] of longint;
i,j,k,n:longint;
ans:int64;
procedure merge(l,r:longint);
var i,j,k,mid:longint;
begin
mid:=(l+r) shr 1;
if l>=r then exit;
merge(l,mid);
merge(mid+1,r);
i:=l; j:=mid+1; k:=l;
repeat
if a[i]>a[j] then begin
b[k]:=a[j];
ans:=ans+mid-i+1;
inc(j);
inc(k);
end
else begin
b[k]:=a[i];
inc(i);
inc(k);
end;
until ((i>mid) or (j>r));
while (i<=mid) do
begin
b[k]:=a[i];
inc(i);
inc(k);
end;
while (j<=r) do
begin
b[k]:=a[j];
inc(j);
inc(k);
end;
for i:=l to r do a[i]:=b[i];
end;
procedure main;
begin
readln(n);
while (n<>0) do
begin
ans:=0;
for i:=1 to n do read(a[i]);
merge(1,n);
writeln(ans);
readln(n);
end;
END;
begin
main;
end.