sgu180

其实可以写归并排序求逆序对
但以前写过,就改写了离散化+树状数组

c++离散化真心写得短


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>


const int MAXN = 100000;

int a[MAXN] = {0} , b[MAXN] = {0}, n,  *p;
int sum[MAXN] = {0} , size = 0;

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

int count(int x)
{
    int ret = 0 ;

    while(x > 0)
    {
        ret += sum[x];
        x -= lowbit(x);
    }
    return ret;
}
void add(int x ,const int &val)
{
    while(x <= size)
    {
        sum[x] += val;
        x += lowbit(x);
    }
}

int main()
{
    long long ans = 0;

#ifndef ONLINE_JUDGE    
    freopen("sgu180.in","r",stdin);
    freopen("sgu180.out","w",stdout);
#endif

    scanf("%d",&n);

    for(int i = 1; i <= n; i++)
     { scanf("%d",&a[i]); b[i] = a[i];}

    std::sort(b + 1, b +n +1);

    p =  std::unique(b + 1, b + n +1); 
    size = p - (b + 1) ;  

    for(int i = 1; i <= n; i++)
        a[i] = std::lower_bound(b + 1, p , a[i]) - b;    

    for(int i = 1; i <= n; i++)
    {  
        ans += (count(size) - count(a[i]));
        add(a[i] , 1);
    }

    printf("%I64d",ans);

#ifndef ONLINE_JUDGE
    fclose(stdin);  
    fclose(stdout);
#endif
    return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值