题目链接:http://poj.org/problem?id=2299
Description

Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source
Waterloo local 2005.02.05
题目要求用归并排序求最少的交换次数,其实就是求逆序对的个数。。
第一种:
归并排序nlogn...(PS 最近在改代码风格,师兄说我之前写的太挫了...)
推荐一个归并排序算法讲解: http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html
代码:
第二种:
树状数组,参考大牛的 http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html
代码:
题目要求用归并排序求最少的交换次数,其实就是求逆序对的个数。。
第一种:
归并排序nlogn...(PS 最近在改代码风格,师兄说我之前写的太挫了...)
推荐一个归并排序算法讲解: http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html
代码:
#include <stdio.h>
#include <string.h>
const int maxn = 500005;
int a[maxn],b[maxn];
long long ans = 0;
void mergesort(int low, int m, int high)
{
int i = low, j = m + 1;
int k = 0;
while(i <= m && j <= high) //左右两边比较,取较小的那个
{
if(a[i] <= a[j])
{
b[k++] = a[i++];
}
else
{
b[k++] = a[j++];
ans += (long long)m - i + 1; //统计逆序对的个数
}
}
while(i <= m) //左边不空
b[k++] = a[i++];
while(j <= high) //右边不空
b[k++] = a[j++];
for(i = low, k = 0; i <= high; i++, k++) //将排序后的放回原来数组里
a[i] = b[k];
}
void merge(int low, int high) //递归
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
merge (low, mid);
merge (mid+1, high);
mergesort(low, mid, high); //分治
}
}
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while(scanf("%d ", &n) && n)
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
ans = 0;
for(int i = 1; i <= n; i++)
scanf("%d ", &a[i]);
merge(1, n);
// for(int i = 1; i <= n; i++)
// printf("%d ", a[i]);
printf("%lld\n",ans);
}
return 0;
}
第二种:
树状数组,参考大牛的 http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 500005;
int n;
int a[maxn], b[maxn];
struct node
{
int val, id;
}in[maxn];
int lowbit(int t)
{
return t & (-t);
}
void add(int t, int d)
{
for(int i = t; i <= n; i += lowbit(i))
a[i] += d;
}
int get_sum(int t)
{
int s = 0;
for(int i = t; i >= 1; i -= lowbit(i))
s += a[i];
return s;
}
int cmp(node a, node b)
{
return a.val < b.val;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d ", &n) && n)
{
memset(b, 0, sizeof(b));
for(int i = 1; i <= n; i++)
{
scanf("%d ", &in[i].val);
in[i].id = i;
}
sort (in + 1, in + 1 + n, cmp);
for(int i = 1; i <= n; i++)
b[in[i].id] = i;
__int64 ans = 0;
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i++)
{
add(b[i], 1);
ans += i - get_sum(b[i]);
}
printf("%I64d\n",ans);
}
return 0;
}