poj 2299 树状数组+离散化 or 归并排序 求逆序对

题意:

给一个n < 500,000的数组,其中0 ≤ a[i] ≤ 999,999,999。

求逆序对。


解析:

树状数组+离散化是一种方法。

归并排序是一种方法。

线段树+离散化应该也ok。


树状数组:

离散化完了以后:(以下转载)

假设输入的数组是9 1 0 5 4, 离散后的结果aa[] = {5,2,1,4,3};

在离散结果中间结果的基础上,那么其计算逆序数的过程是这么一个过程。

1,输入5,   调用upDate(5, 1),把第5位设置为1

1 2 3 4 5

0 0 0 0 1

计算1-5上比5小的数字存在么? 这里用到了树状数组的getSum(5) = 1操作,

现在用输入的下标1 - getSum(5) = 0 就可以得到对于5的逆序数为0。

2. 输入2, 调用upDate(2, 1),把第2位设置为1

1 2 3 4 5

0 1 0 0 1

计算1-2上比2小的数字存在么? 这里用到了树状数组的getSum(2) = 1操作,

现在用输入的下标2 - getSum(2) = 1 就可以得到对于2的逆序数为1。

3. 输入1, 调用upDate(1, 1),把第1位设置为1

1 2 3 4 5

1 1 0 0 1

计算1-1上比1小的数字存在么? 这里用到了树状数组的getSum(1) = 1操作,

现在用输入的下标 3 - getSum(1) = 2 就可以得到对于1的逆序数为2。

4. 输入4, 调用upDate(4, 1),把第5位设置为1

1 2 3 4 5

1 1 0 1 1

计算1-4上比4小的数字存在么? 这里用到了树状数组的getSum(4) = 3操作,

现在用输入的下标4 - getSum(4) = 1 就可以得到对于4的逆序数为1。

5. 输入3, 调用upDate(3, 1),把第3位设置为1

1 2 3 4 5

1 1 1 1 1

计算1-3上比3小的数字存在么? 这里用到了树状数组的getSum(3) = 3操作,

现在用输入的下标5 - getSum(3) = 2 就可以得到对于3的逆序数为2。

6. 0+1+2+1+2 = 6 这就是最后的逆序数



代码:

树状数组+离散化:7932K391MS

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 500000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

struct Node
{
    int num;
    int index;
} t[maxn];
int a[maxn];
int c[maxn];
int n;

bool cmp(Node a, Node b)
{
    return a.num < b.num;
}

int lowbit(int x)
{
    return x & -x;
}

void update(int x, int num)
{
    while (x <= n)
    {
        c[x] += num;
        x += lowbit(x);
    }
}

int query(int x)
{
    int res = 0;
    while (x)
    {
        res += c[x];
        x -= lowbit(x);
    }
    return res;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    while (~scanf("%d", &n) && n)
    {
        ///离散化
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &t[i].num);
            t[i].index = i;
        }
        sort(t + 1, t + n + 1, cmp);
        for (int i = 1; i <= n; i++)
        {
            a[t[i].index] = i;
        }

        memset(c, 0, sizeof(c));
        LL ans = 0;
        for (int i = 1; i <= n; i++)
        {
            update(a[i], 1);
            ans = ans + (i - query(a[i]));
        }
        printf("%lld\n", ans);
    }
    return 0;
}

归并排序:7736K516MS

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>

#define LL long long

using namespace std;
const int maxn = 5 * 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

LL a[maxn];
LL ans;

LL t[maxn];
void Reverse(LL lo, LL mi, LL hi)
{
    LL i = lo;
    LL j = mi + 1;
    LL k = lo;
    while (i <= mi && j <= hi)
    {
        if (a[i] <= a[j])
        {
            t[k++] = a[i++];
        }
        else
        {
            t[k++] = a[j++];
            ans += (mi - i + 1);
        }
    }
    while (i <= mi)
    {
        t[k++] = a[i++];
    }
    while (j <= hi)
    {
        t[k++] = a[j++];
    }
    for (i = lo; i <= hi; i++)
    {
        a[i] = t[i];
    }
}

void Merge(LL A[], LL lo, LL hi)
{
    if (lo < hi)
    {
        LL mi = (lo + hi) >> 1;
        Merge(A, lo, mi);
        Merge(A, mi + 1, hi);
        Reverse(lo, mi, hi);
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int n;
    while (scanf("%d", &n) && n)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%lld", &a[i]);
        }
        ans = 0;
        Merge(a, 0, n - 1);
        printf("%lld\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值