本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y)
实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大,所以使用树状数组时需要先离散化
离散化后排序有两种方式,一种是先按value排,再按id排,另一种是按value排后,把重复的统一化
这里采用第一种方式
Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1882 Accepted Submission(s): 686
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.
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.
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
3
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int v, id;
bool operator < (const node &a) const
{
if (v == a.v)
return id < a.id;
return v < a.v;
}
}t[]; int ch[];
int ch1[];
int n; void update(int x)
{
int temp = x;
while (temp <= n)
{
ch[temp]++;
temp += temp & (-x);
}
} int getsum(int x)
{
int sum = ;
while (x)
{
sum += ch[x];
x -= x & (-x);
}
return sum;
} int main()
{
ios::sync_with_stdio(false);
int a, b;
while (cin>> n >> a >> b)
{
memset(ch, , sizeof(ch));
for (int i = ; i <= n; i++)
{
cin >> t[i].v;
t[i].id = i;
} sort(t+,t+n+); long long ans = ;
for (int i = ; i <= n; i++)
{
update(t[i].id);
ans += i - getsum(t[i].id);
}
ans = min(ans*a, ans*b);
cout << ans << endl;
}
return ;
}