2018 Multi-University Training Contest 2 1010
Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 752 Accepted Submission(s): 291
Problem Description
Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
Input
There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
Output
For every test case, a single integer representing minimum money to pay.
Sample Input
3 233 666
1 2 3
3 1 666
3 2 1
Sample Output
0 3
Source
2018 Multi-University Training Contest 2
Recommend
chendu
题意:给出一个长为 n 的数列,有两种花费 x 和 y
如果最终 数列里有 m 个逆序对 则你需要花费 m * x
但是你也可以花费 y 元对任意两个相邻的数做一次交换
问你的最小花费是多少
解:
首先理解一个逆序对的原理,如果一个数列里有 n 个逆序对,那么我们只需要交换 n 次就可以消除所有逆序对
证明的话,自己拿纸笔写一写就知道了
所以这道题就很明确了,只需要找出数列里逆序对的个数,然后乘以 min(x,y) 即可
求逆序对有两种方法,一种是用归并排序的,个人不喜欢这种方法
另一种是用树状数组求逆序对,把数列按照顺序把值插入,比如数列 3 2 1
1.数列下标为1的值是3,在树状数组3号位置加1,然后用下标减去 getSum(3)
getSum(3)表示 树状数组中3号位前面有多少个值,这里 getSum(3) = 1 ,就是它自己,但是原数列里它的位置是第一个
所以 i - getSum(i) ----> 1 - getSum(3) = 0 所以第一个数产生的逆序对是 0
2.数列下标为2的值是2,在2树状数组号位置加1,然后用下标减去 getSum(2)
树状数组中2号位前面只有它自己,所以 getSum(2) = 1,但是我们知道原数列里面 2号位前面(包括自己)总共是有2个数的
现在 getSum(2) 只有 1,说明 原数列2号位前面有1个比他大的数,所以 i- getSum(i) = 2 - getSum(2) = 1 ,所以逆序对为1
3.数列下标为3的值是1,在树状数组1号位加一,然后用下标减去 getSum(1) = 2
同理得出逆序对为 2
所以总的逆序对数为 0 + 1 + 2 = 3
不过这道题的数比较大[−1e9,1e9],但是实际上并没有那么多个数,我们只需要知道大小关系,所以离散化一下即可
代码如下
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 100005;
#define ll long long
#define lowbit(x) (x & (-x))
#define mem(a,x) memset(a,x,sizeof(a))
int n,a[maxn],b[maxn],tree[maxn];
void update(int x,int v){
while(x <= n){
tree[x] += v;
x += lowbit(x);
}
}
ll getSum(int x){
ll tmp = 0;
while(x){
tmp += tree[x];
x -= lowbit(x);
}
return tmp;
}
void discre(){ //离散化
map<int,int>ma;
sort(b + 1,b + 1 + n);
int cnt = 1;
for(int i = 1;i <= n;i++){
if(i == 1){
ma[b[i]] = cnt;
}else{
if(b[i] != b[i - 1]){
ma[b[i]] = ++cnt;
}else{
ma[b[i]] = cnt;
}
}
}
for(int i = 1;i <= n;i++){
a[i] = ma[a[i]];
}
}
int main(){
int x,y;
while(scanf("%d %d %d",&n,&x,&y) != EOF){
mem(tree,0);
for(int i = 1;i <= n;i++){
scanf("%d",&a[i]);
b[i] = a[i];
}
discre();
ll ans = 0;
for(int i = 1;i <= n;i++){
update(a[i],1);
ans += i - getSum(a[i]);
}
printf("%lld\n",ans * min(x,y));
}
return 0;
}