2018 Multi-University Training Contest 2 1010 Swaps and Inversions

 思路很简单, 求逆序数然后乘上x, y里面较大的那个,但是一开始只会暴力求法。。。队友写了并归然后赛后也学了并归算法,嗯感觉有一点二分的意思,据dalao说还有树状数组的方法,看了看博客还是不大懂。。。跟二进制有关就容易晕。。。要继续加油啊。

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

#include<stdio.h>
#include<string.h>

int a[100005], b[100005];

long long count;//记录逆序数

void merge(int low, int mid, int high)
{
    int i = low, j = mid + 1, k = low;
    while ((i <= mid) && (j <= high))
    {
        if (a[i] <= a[j])
        {
            b[k++] = a[i++];
        }
        else
        {
            b[k++] = a[j++];
            count += mid - i + 1;//记录逆序数
        }
    }
    while (i <= mid)
        b[k++] = a[i++];
    while (j <= high)
        b[k++] = a[j++];
    for (int i = low; i <= high; i++)
        a[i] = b[i];


}
void sort(long long low, long long high)
{
    int mid = (low + high) / 2;
    int x, y, z;
    if (low >= high)
        return;
    sort(low, mid);
    sort(mid + 1, high);
    merge(low, mid, high);
    return;

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

猜你喜欢

转载自blog.csdn.net/blackmail3/article/details/81275231