Swaps and Inversions HDU - 6318 树状数组+离散化

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swaps and Inversions HDU - 6318 树状数组+离散化相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int a[N];
int ra[N];
int tr[N];
int n,x,y;
int sz;
int lowbit(int x)
{
    return x&-x;
} 
void add(int x,int c)
{
    for(int i=x;i<=n;i+=lowbit(i))
        tr[i]+=c;
}
int sum(int x)
{
    int res=0;
    for(int i=x;i;i-=lowbit(i))
        res+=tr[i];
    return res;
}
int main()
{
    while(scanf("%d%d%d", &n, &x, &y) != EOF)
    {
        memset(tr,0,sizeof tr);
        for(int i=1;i<=n;i++)
        {
            scanf("%d", &a[i]);
            ra[i]=a[i];
        }
        //离散化 
        sort(ra+1,ra+1+n);
        sz=unique(ra+1,ra+1+n)-ra-1;
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            int num=lower_bound(ra+1,ra+1+n,a[i])-ra;
            add(num,1);
            ans+=i-sum(num);
        }
        if(x<y)
            ans=ans*x;
        else
            ans=ans*y;
        printf("%lld
", ans);
    }
}

 

以上是关于Swaps and Inversions HDU - 6318 树状数组+离散化的主要内容,如果未能解决你的问题,请参考以下文章

775. Global and Local Inversions

775. Global and Local Inversions

[leetcode-775-Global and Local Inversions]

[LeetCode] Global and Local Inversions 全局与局部的倒置

PAT-T1027 Larry and Inversions (树状数组)

leetcode 775. Global and Local Inversions ---找规律