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 1i<jn 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.
1n,x,y100000, 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

题意:每次交换会花费y的费用,有一个逆序对的会花费x的费用,求最小的费用

题解:找出逆序对的个数,然后求出最小费用即可

逆序对的个数可以用归并排序时的交换次数表示

那么我们只需要在归并排序的时候得到交换次数即可

代码如下:

HDU 多校对抗赛第二场 1010 Swaps and Inversions_hduHDU 多校对抗赛第二场 1010 Swaps and Inversions_hdu_02
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+5;
int a[maxn];
int c[maxn]; 
long long ans=0;
void Msort(int l,int r){
    int mid=(l+r)/2;
    int i,j,tmp;
    if(r>l){
        Msort(l,mid);
        Msort(mid+1,r);
        tmp=l;
        for(i=l,j=mid+1;i<=mid&&j<=r; ){
            if(a[i]>a[j]){
                c[tmp++]=a[j++];
                ans+=mid-i+1;
            }else{
                c[tmp++]=a[i++];
            }
        }
        if(i<=mid) for(;i<=mid;) c[tmp++]=a[i++];
        if(j<=r) for(;j<=r;) c[tmp++]=a[j++];
        for(i=l;i<=r;i++) a[i]=c[i];
    }
}

int main(){
    int n,x,y,t;
    while(scanf("%d%d%d",&n,&x,&y) !=EOF){
        ans=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        Msort(0,n-1);
        for(int i)
        long long ans1=min(x,y)*ans;
        printf("%lld\n",ans1);
    }
    return 0;
}
View Code

 

每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi