Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1489 Accepted Submission(s): 542
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
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
int n, c[mn];
struct node
{
int v, id;
} t[mn];
bool cmp ( const node& a, const node& b )
{
if ( a.v != b.v )
return a.v > b.v; /// 数值从大到小
return a.id > b.id; /// 下标从大到小
}
void update ( int t )
{
while ( t <= n )
{
c[t]++;
t += t & ( -t );
}
}
long long query ( int t )
{
long long res = 0;
while ( t > 0 )
{
res += c[t];
t -= ( t & -t );
}
return res;
}
int main()
{
int x, y;
while ( ~scanf ( "%d %d %d", &n, &x, &y ) )
{
for ( int i = 1; i <= n; i++ )
{
int a;
scanf ("%d", &a );
t[i].v = a;
t[i].id = i;
} /// 保存数值及下标
memset ( c, 0, sizeof c );
sort ( t + 1, t + n + 1, cmp );
long long ans = 0;
for ( int i = 1; i <= n; i++ ) /// 从最大的数开始
{
ans += query ( t[i].id );
/// 查询这个数之前的位置有几个1 (大于当前)
update ( t[i].id );
/// 将这个位置记1
}
printf ( "%lld\n", ans * min ( x, y ) );
}
return 0;
}