[ HDU6318 ] Swaps and Inversions

#include<cstdio>
#include<algorithm>
#define ll long long
#define N 200005
using namespace std;
int tr[N],b[N],n,x,y;
ll ans;
struct he{
    int d,p;
}a[N];
bool cmp(he a,he b){
    return a.d<b.d;
}
int lowbit(int x){
    return x&-x;
}
void add(int x,int u){
    for(;x<=n;x+=lowbit(x)) tr[x]+=u;
}
int get(int x){
    int tmp=0;
    for(;x;x-=lowbit(x)) tmp+=tr[x];
    return tmp;
}
int main(){
    while(scanf("%d%d%d",&n,&x,&y)!=EOF){
        for(int i=1;i<=n;i++) tr[i]=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i].d),a[i].p=i;
        sort(a+1,a+1+n,cmp);
        int now=0;
        a[0].d=a[1].d+1;
        for(int i=1;i<=n;i++) {
                if(a[i].d!=a[i-1].d)now++;
                 b[a[i].p]=now;
        }
        ans=0;
        for(int i=n;i>=1;i--){
            int x=get(b[i]-1);
            ans+=x;
            add(b[i],1);
        }
        if(y<x)printf("%lld\n",ans*y);else printf("%lld\n",ans*x);
    }
}

Leave a Reply

Your email address will not be published.