题目链接: http://poj.org/problem?id=2299
题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数。
利用归并排序求解,内存和耗时都比较少, 但是有编码难度。。
二叉排序树,内存巨大,时间复杂度高,但是非常好写。。
归并排序版本:
#include <stdio.h>
#include <string.h>
long long merge_arr(int arr[], int first, int mid, int last, int tmp[])
{
long long ret = ;
int i = first, j = mid+, k = ;
while(i <= mid && j <= last)
{
if(arr[i] < arr[j])
{
tmp[k++] = arr[i++];
ret += j - mid - ;
}
else tmp[k++] = arr[j++];
}
while(i <= mid)
{
tmp[k++] = arr[i++];
ret += last - mid;
}
while(j <= last)
tmp[k++] = arr[j++];
for(i = ; i < k; i++)
arr[first+i] = tmp[i];
return ret;
} long long merge_sort(int arr[], int first, int last, int tmp[])
{
long long ret = ;
if(first < last)
{
int mid = (first + last) / ;
ret += merge_sort(arr, first, mid, tmp);
ret += merge_sort(arr, mid+, last, tmp);
ret += merge_arr(arr, first, mid, last, tmp);
}
return ret;
} int n, num[], tmp[];
int main()
{
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i < n; i++)
scanf("%d", &num[i]);
printf("%I64d\n", merge_sort(num, , n-, tmp));
}
return ;
}
二叉排序树版本:
#include <stdio.h>
#include <stdlib.h> struct node
{
int data, cnt_right;
struct node *left, *right;
}; long long ans; void build(struct node *&p, int k)
{
if(p == NULL)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = k;
p->cnt_right = ;
p->left = p->right = NULL;
}
else if(p->data > k)
{
ans += p->cnt_right + ;
build(p->left, k);
}
else
{
p->cnt_right++;
build(p->right, k);
}
} void del(struct node *p)
{
if(p == NULL)return;
del(p->left);
del(p->right);
free(p);
} int main()
{
int n, x;
while(scanf("%d", &n) != EOF && n)
{
ans = ;
struct node *root = NULL;
while(n--)
{
scanf("%d", &x);
build(root, x);
}
del(root);
printf("%I64d\n", ans);
}
return ;
}