2299 求逆序数对 归并排序/树状数组

n this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output
0 1 4 5 9 .

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


逆序数对,一开始是在归并排序那了解到的,现在知道还可以用树状数组做,当然也可以用线段树(能用树状数组解决的,都能用线段树做,反过来不行)而且,后两种方法的效率要比归并排序高很多,题目数据的范围太大,树状数组那里用了离散化。直接上代码吧

//归并排序

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int N = 5e5 + 3;

long long cnt;

void Merge(long long a[], int l, int mid, int r)
{
    int b[N], i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r)
    {
        if(a[i] <= a[j])
            b[k++] = a[i++];
        else
        {
            b[k++] = a[j++];
            cnt += mid - i + 1;  //注意理解这里,结合归并排序的原理
        }
    }
    while(i <= mid) b[k++] = a[i++];
    while(j <= r) b[k++] = a[j++];
    for(i = 0; i < k; i++)
        a[l + i] = b[i];
}
void MergeSort(long long a[], int l, int r)
{
    if(l < r)
    {
        int mid = (l + r) >> 1;
        MergeSort(a, l, mid);
        MergeSort(a, mid + 1, r);
        Merge(a, l, mid, r);
    }
}
int main()
{
    long long a[N];
    int n;
    while(~scanf("%d", &n) && n)
    {
        for(int i = 0; i < n; i++)
            scanf("%lld", &a[i]);
        cnt = 0;
        MergeSort(a, 0, n - 1);
        printf("%lld\n", cnt);
    }   
    return 0;
}

//树状数组

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lowbit(x) (x & (-x))

using namespace std;
const int N = 5e5 + 3;

struct node
{
    long long d;
    int p;
}t[N];
int c[N], a[N];

void Update(int k)
{
    for(int i = k; i < N; i += lowbit(i))
        c[i]++;
}
int GetSum(int k)
{
    int sum = 0;
    for(int i = k; i > 0; i -= lowbit(i))
        sum += c[i];
    return sum;
}
bool cmp(node a, node b)
{
    return a.d < b.d;
}
int main()
{
    int n;
    while(~scanf("%d", &n) && n)
    {
        memset(c, 0, sizeof(c));
        for(int i = 1; i <= n; i++)
        {
            scanf("%lld", &t[i].d);
            t[i].p = i;
        }
        sort(t, t + n + 1, cmp);     //排序,接下来是离散化
        for(int i = 1; i <= n; i++)
        {
            if(i == 1 || t[i].d != t[i - 1].d)
                a[t[i].p] = i;
            else
                a[t[i].p] = a[t[i - 1].p];
        }
        long long cnt = 0;
        for(int i = 1; i <= n; i++) 
        {
            Update(a[i]);
            cnt += i - GetSum(a[i]); //GetSum(x)得到的是x左边比它小的个数
        }
        printf("%lld\n", cnt);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick tree)是一种用于高效解数列前缀和(Prefix Sum)问题的数据结构,而逆序对也是一种经典的问题。 假设有个长度为 n 的数组 a,我们要其中逆序对的数量,即所有 i < j 且 a[i] > a[j] 的 (i,j) 对数。这个问题可以用归并排序的思想解,但是这里我们介绍一种使用树状数组的解法。 思路如下: 1. 将原数组 a 复制一份并排序,得到新数组 b,将 b 中的每个元素在原数组 a 中的下标记录在数组 c 中。 2. 从右往左遍历 a,对于每个元素 a[i],在树状数组中查询前缀和 sum(c[i]-1),即小于 a[i] 的元素个数,将结果累加到逆序对计数器 ans 中。 3. 将 a[i] 在数组 c 中对应的位置在树状数组中更新为 1。 下面是使用 Python 实现的代码: ```python def lowbit(x): return x & -x def update(bit, x, val): n = len(bit) while x <= n: bit[x] += val x += lowbit(x) def query(bit, x): res = 0 while x > 0: res += bit[x] x -= lowbit(x) return res def count_inversions(a): n = len(a) b = sorted(a) c = {v: i+1 for i, v in enumerate(b)} bit = [0] * (n+1) ans = 0 for i in range(n-1, -1, -1): ans += query(bit, c[a[i]]-1) update(bit, c[a[i]], 1) return ans ``` 其中,lowbit 函数是计算 x 的最低位 1 所代表的值;update 函数是树状数组的更新操作;query 函数是树状数组的查询操作;count_inversions 函数是主函数,用于计算逆序对数量。 时间复杂度为 O(nlogn)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值