#include <iostream>#include <stdio.h>usingnamespacestd;
int n;
longlong count1;
int s1[500000], s2[5000000];// s1为原数组,s2为临时数组void mergeSort(int a, int b)
{
if (a<b)
{
int mid = (a + b) / 2;
mergeSort(a, mid);
mergeSort(mid + 1, b);
int i = a, j = mid + 1, k = a;
while (i <= mid&&j <= b)
if (s1[i] <= s1[j])// 此处为稳定排序的关键,不能用小于
s2[k++] = s1[i++];
else
{
s2[k++] = s1[j++];
count1 += j - k;// 每当后段的数组元素提前时,记录提前的距离
}
while (i <= mid)
s2[k++] = s1[i++];
while (j <= b)
s2[k++] = s1[j++];
for (i = a; i <= b; i++)// 写回原数组
s1[i] = s2[i];
}
return;
}
int main()
{
while (cin >> n&&n)
{
for (int i = 0; i < n; i++)
cin >> s1[i];
count1 = 0;
mergeSort(0, n - 1);
printf("%lld\n", count1);
}
return0;
}