AC过的题现在不会了/kk

P1908 逆序对

cmaths @ 2022-08-06 08:41:40

code

题目是AcWing的,不过问题不大,主要是想让大佬帮忙看下逆序对

非常感谢orz


by irris @ 2022-08-06 08:47:50

谔谔,对不起,我再看看


by kqQwQ @ 2022-08-06 08:53:12

@xjr300098 建议您再去把归并排序复习一遍做

#include <cstdio>
#define int long long

int a[500005], res[500005];
int ms(int l, int r)
{
    if(l == r)
    {
        return 0;
    }
    int mid = (l + r) / 2, i = l, j = mid + 1, k = 1;
    int ans = ms(l, mid) + ms(mid + 1, r);
    while(i <= mid && j <= r)
    {
        if(a[i] <= a[j])
        {
            res[k++] = a[i++];
        }
        else
        {
            ans += (mid - i + 1);
            res[k++] = a[j++];
        }
    }
    while(i <= mid)
    {
        res[k++] = a[i++];
    }
    while(j <= r)
    {
        res[k++] = a[j++];
    }
    for (int i = l, j = 1; i <= r; ++i, ++j) 
    {
        a[i] = res[j];
    }
    return ans;
}
signed main()
{
    int n;
    while(scanf("%lld", &n) == 1)
    {
        if(n == 0)
        {
            break;
        }
        for(int i = 1; i <= n; i++)
        {
            scanf("%lld", &a[i]);
        }
        printf("%lld\n", ms(1, n));
    }
    return 0;
}

by cmaths @ 2022-08-06 08:59:17

@Keqing_qwq wssb

谢谢您


by irris @ 2022-08-06 09:55:54

没必要,可以直接用 BIT 了


|