Swaps and Inversions

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 \leq i < j \leq n$ and $a_i > a_j$.

 

 

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 \leq n,x,y \leq 100000$, numbers in the sequence are in $[-10^9,10^9]$. 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

 

问题分析:

       这道题用到的是归并排序,通过这种方法寻找逆序数,然后计算金额就可以了。

归并排序:

把一个数组细分成单个元素,每个元素看做一个数组,在把这些元素数组按顺序不断合并,1个和并成2个,2个合并成4个…直到全部合并完成。 

图片样式

代码实现如下:

#include<iostream>
using namespace std;
void merge(int *data, int start, int mid, int end, int *result)//合并函数
{
    int i, j, k;
    i = start;
    j = mid + 1;                        //避免重复比较data[mid]
    k = 0;
    while (i <= mid && j <= end)        //把两个数组从第一个开始比较小的加入整理数组直到一个数组都加入
    {
        if (data[i] <= data[j])
            result[k++] = data[i++];
        else
            result[k++] = data[j++];
    }
    while (i <= mid)        //将剩下的的全部加入整理后的数组
        result[k++] = data[i++];
    while (j <= end)
        result[k++] = data[j++];

    for (i = 0; i < k; i++)         //把整理后的数组替代掉之前的未整理的
        data[start + i] = result[i];    //注意,应从data[start+i]开始赋值
}
void merge_sort(int *data, int start, int end, int *result)
{
    if (start < end)    //细分到每一个元素都为一个数组后开始合并
    {
        int mid = (start + end) / 2;
        merge_sort(data, start, mid, result);                    //对左边进行排序
        merge_sort(data, mid + 1, end, result);                  //对右边进行排序
        merge(data, start, mid, end, result);                    //把排序好的数据合并
    }
}

 

 

 

这道题的代码应该是实现最小金额的输出,代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <math.h>
#include <set>
using namespace std;
#define inf 0x3f3f3f3
const int mod = 1e9+7;
const int N = 1e5+7;
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;

int a[N],t[N],n,x,y;

inline ll merge(int l,int mid,int r)
{
    int i=l,j=mid+1,k=0;
    ll an=0;
    while(i<=mid && j<=r){
        if(a[i]>a[j]){
            an+=(mid-i+1);//this step 
            t[k++]=a[j++];
        }else{
            t[k++]=a[i++];
        }
    }
    while(i<=mid){
       t[k++]=a[i++];
    } 
    while(j<=r) t[k++]=a[j++];
    for(int g=0;g<k;g++) a[l+g]=t[g];
    return an;
}

inline ll query(int l,int r){
    ll ans=0;
    if(l<r){
        int mid=(l+r)>>1;
        ans+=query(l,mid);
        ans+=query(mid+1,r);
        ans+=merge(l,mid,r);
    }
    return ans;
}

int main(){
    while (~scanf("%d %d %d",&n,&x,&y))
    {
        for(int i=1;i<=n;i++)
          scanf("%d",&a[i]);
        ll ans = query(1,n);
        if (x > y)
            ans *= y;
        else ans *= x;
        printf("%lld\n",ans);
    }
    return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值