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)(i,j) which 1≤i<j≤n1≤i<j≤n and ai>ajai>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≤1000001≤n,x,y≤100000, numbers in the sequence are in [−109,109][−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
x,y是两种付费方式,只要取它小的那个就行了
关键的是取它的逆序次数,因为它的范围开的很大10w,正常排序o^2会超时
于是用了归并排序
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt;
int arr[100005];
void Merge(int* arr,int* tmp,int left,int right,int rightEnd)
{
int leftEnd=right-1;
int start=left;
while(left<=leftEnd&&right<=rightEnd)
{
if(arr[left]>arr[right])
{
tmp[start++]=arr[right++];
cnt+=(leftEnd-left+1);
}
else
{
tmp[start++]=arr[left++];
}
}
while(left<=leftEnd)
{
tmp[start++]=arr[left++];
}
while(right<=rightEnd)
{
tmp[start++]=arr[right++];
}
}
void MergeSort(int* arr,int* tmp,int n,int length)
{
int i;
for(i=0; i<=n-2*length; i+=2*length)
{
Merge(arr,tmp,i,i+length,i+2*length-1);
}
if(i+length<n)
{
Merge(arr,tmp,i,i+length,n-1);
}
else
{
for(int j=i; j<n; j++)
{
tmp[j]=arr[j];
}
}
}
void Merge_Sort(int* arr,int n)
{
int lenght = 1;
int* tmp=(int *)malloc(sizeof(int)*n);
while (lenght < n)
{
MergeSort(arr,tmp,n,lenght);
lenght *= 2;
MergeSort(tmp,arr,n,lenght);
lenght *= 2;
}
free(tmp);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(arr,0,sizeof(arr));
int x,y;
scanf("%d%d",&x,&y);
for(int i=0; i<n; i++)
scanf("%d",&arr[i]);
cnt=0;
Merge_Sort(arr,n);
if(x<y)
printf("%lld\n",cnt*x);
else
printf("%lld\n",y*cnt);
}
return 0;
}