链接
http://acm.hdu.edu.cn/showproblem.php?pid=6318
题解
这是今天自己做出来的第一道题T_T
注意只要逆序对存在,就肯定有相邻的逆序对(反证法),所以每次交换都可以去掉一个逆序对
所以只要交换逆序对更便宜我就一直交换下去直到不存在逆序对,否则我就干脆不交换
假设逆序对有
c
n
t
cnt
cnt个,那么答案就是
x
×
c
n
t
x\times cnt
x×cnt和
y
×
c
n
t
y\times cnt
y×cnt取较小值
代码
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long ll;
struct BIT
{
ll bit[maxn], n;
void init(int N){n=N;for(int i=1;i<=n;i++)bit[i]=0;}
ll lowbit(ll x){return x&(-x);}
void add(ll pos, ll v)
{
for(;pos<=n;pos+=lowbit(pos))bit[pos]+=v;
}
ll sum(ll pos)
{
ll ans(0);
for(;pos;pos-=lowbit(pos))ans+=bit[pos];
return ans;
}
}bit;
struct Lisan
{
int tmp[maxn], tot;
void clear(){tot=0;}
void insert(int x){tmp[++tot]=x;}
void run()
{
sort(tmp+1,tmp+tot+1);
tot=unique(tmp+1,tmp+tot+1)-tmp-1;
}
void lisan(int *a, int len)
{
for(int i=1;i<=len;i++)a[i]=lower_bound(tmp+1,tmp+tot+1,a[i])-tmp;
}
int lisan(int x)
{
return lower_bound(tmp+1,tmp+tot+1,x)-tmp;
}
}ls;
int a[maxn];
int main()
{
ios::sync_with_stdio(false);
int N, x, y, i;
while(cin>>N>>x>>y)
{
ls.clear();
for(i=1;i<=N;i++)cin>>a[i], ls.insert(a[i]);
ls.run();
ls.lisan(a,N);
ll cnt=0;
bit.init(N);
for(i=1;i<=N;i++)
{
bit.add(a[i],+1);
cnt+=i-bit.sum(a[i]);
}
cout<<min(x,y)*cnt<<'\n';
}
return 0;
}