树状数组求解逆序数

数列的逆序数可以使用归并排序求解,亦可以使用树状数组解决。现在献上两题,用树状数组求解逆序数。

POj 2299 Ultra-QuickSort
http://poj.org/problem?id=2299
大意:一个排列经过多少次交换能够成为排好序的结果。
分析:之前用归并排序做过, http://blog.csdn.net/thearcticocean/article/details/48057293
这次练习数据结构。离散(映射)+树状数组
例如:1 9 8 4 5 --->  1 5 4 2 3
依据数值的大小重新确定值,节省空间。
接着就是插入确定逆序:
_ _ _ _ _
1 _ _ _ _    (1~0)
1 _ _ _ 1    (5~3)
1 _ _ 1 1    (4~2)
1 1 _ 1 1    (2~0)
1 1 1 1 1    (3~0)
统计数字前面的空格总和——逆序

最后相加即可。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=5e5+10;
typedef long long LL;
int d[N];  //  disperse array
LL c[N];  
struct node{
   int u,order;
}a[N];
int cmp(node t1,node t2){
    return t1.u<t2.u;
}
int lowbit(int dex){
    return dex&(dex^(dex-1));
}
LL sum(int dex){
    LL ans=0;
    while(dex>0){
        ans=ans+c[dex];
        dex=dex-lowbit(dex);
    }
    return ans;
}
int main(){
    //freopen("cin.txt","r",stdin);
    int t,len;
    while(~scanf("%d",&len)&&len){
    for(int i=0;i<len;i++){
        scanf("%d",&a[i].u);   
        a[i].order=i+1;
    }
        sort(a,a+len,cmp);
        for(int i=0;i<len;i++){
        d[a[i].order]=i+1;
    }
        memset(c,0,sizeof(c));
    LL ans=0;
    for(int i=1;i<=len;i++){
            int t=d[i];
        while(t<=len){
            c[t]++;    // update
        t=t+lowbit(t);
        }
            ans=ans+d[i]-sum(d[i]); //逆序数
    }
     printf("%lld\n",ans);
    }
    return  0;
}

nyist 117 求逆序数
http://acm.nyist.net/JudgeOnline/problem.php?pid=117
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。 现在,给你一个N个元素的序列,请你判断出它的逆序数是多少。

比如 1 3 2 的逆序数就是1。

(数字可以相等)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e6+10;
typedef long long LL;
int d[N];  //  disperse array
LL c[N];  
struct node{
   int u,order;
}a[N];
int cmp(node t1,node t2){
    return (t1.u<t2.u) || (t1.u==t2.u&&t1.order<t2.order);
}
int lowbit(int dex){
    return dex&(dex^(dex-1));
}
LL sum(int dex){
    LL ans=0;
    while(dex>0){
        ans=ans+c[dex];
    dex=dex-lowbit(dex);
    }
    return ans;
}
int main(){
    //freopen("cin.txt","r",stdin);
    int t,len;
    cin>>t;
    while(t--){
        scanf("%d",&len);
    for(int i=0;i<len;i++){
        scanf("%d",&a[i].u);   
        a[i].order=i+1;
    }
        sort(a,a+len,cmp);
        for(int i=0;i<len;i++){
        d[a[i].order]=i+1;
    }
        memset(c,0,sizeof(c));
    LL ans=0;
    for(int i=1;i<=len;i++){
            int t=d[i];
        while(t<=len){
            c[t]++;    // update
        t=t+lowbit(t);
        }
            ans=ans+d[i]-sum(d[i]); //逆序数
        //cout<<d[i]<<" "<<d[i]-sum(d[i])<<endl;
    }
     printf("%lld\n",ans);
    }
    return  0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值