Swaps and Inversions

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<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
long long 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)!=EOF){
        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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值