求逆序对数【HDU】多校联赛-Swaps and Inversions(归并排序)

Swaps and Inversions

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3588    Accepted Submission(s): 976


 

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

思路:

此题只要求出逆序对数,再用逆序对数乘以x和y中较小的那一个即可。由于题目数据范围过大,普通的暴力肯定超时,于是根据归并排序的原理(点击了解归并排序),此题可以用归并排序来求逆序对的个数,只需要在归并排序中添加一句代码即可。

参考代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
const int MAXN = 100008;
#define Lchild L, Mid
#define Rchild Mid + 1, R
typedef long long ll;

ll ans, a[MAXN], temp[MAXN];
using namespace std;

//归并排序求逆序对
void mergesort(ll L, ll R)
{
    if(L == R)return;
    ll Mid = (L + R) >> 1;
    mergesort(Lchild);
    mergesort(Rchild);

    ll i = L,j = Mid + 1,k = L;
    while(i <= Mid || j <= R)
    {
        if(j > R || (i <= Mid && a[i] <= a[j]))
            temp[k++]=a[i++];
        else
            temp[k++]=a[j++],ans += Mid - i + 1;//计数
    }
    for(i = L; i <= R; i++)
        a[i] = temp[i];
}
int main()
{
    int t;
    ll x, y;
    while(~scanf("%d %lld %lld", &t, &x, &y))
    {
        for(ll i = 0; i < t; i++)
            scanf("%lld", &a[i]);
        ans = 0;
        mergesort(0, t - 1);
        printf("%lld\n", ans * min(x,y));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40907279/article/details/81608393