HDU 6318 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≤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

题意:n个数,求他的逆序数,交换相邻的2个数字花费y,交换逆序数对花费x,就按照字典序排列后花费最小是多少

思路:求逆序数(再次搞错题意系列,把x认为成了反转序列)

1.归并排序求逆序数

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+5;
typedef long long ll;
const ll INF = 1<<60;
ll cnt,k;
int a[maxn],c[maxn];
int b[maxn],d[maxn];

void merge(int* a,int first,int mid,int last,int* c) {
    int i = first,j=mid+1;
    int m = mid,n=last;
    int k = 0;
    while(i<=m||j<=n) {
        if(j>n||(i<=m&&a[i]<=a[j])) {
            c[k++] = a[i++];
        }
        else {
            c[k++] = a[j++];
            cnt += (m-i+1);
        }
    }
    for(i=0;i<k;i++)
        a[first+i] = c[i];
}

void mergeSort(int* a,int first,int last,int* c) {
    if(first<last) {
        int mid = (first+last)/2;
        mergeSort(a,first,mid,c);
        mergeSort(a,mid+1,last,c);
        merge(a,first,mid,last,c);
    }
}

int main()
{
    int n,x,y;
    while(~scanf("%d%d%d",&n,&x,&y)) {
        for(int i=0,j=n-1;i<n;i++,j--){
           scanf("%d",&a[i]);
           b[j]=a[i];
        }
        memset(c,0,sizeof(c));
        cnt=0;
        mergeSort(a,0,n-1,c);
        ll num1=cnt-k;  //逆序数
        if(x<y)
            swap(x,y);
        ll sum=num1*y;
        printf("%lld\n",sum);
    }
    return 0;
}

2.树状数组+离散化求逆序数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;
const int MAXN=500010;
typedef long long ll;
int c[MAXN];
int b[MAXN];
int n,x,y;
struct node
{
    int id;//序号
    int v;
}a[MAXN];
bool cmp(node a,node b)
{
    return a.v<b.v;
}
int lowbit(int x)
{
    return x&(-x);
}
void add(int i,int val)
{
    while(i<=n)
    {
        c[i]+=val;
        i+=lowbit(i);
    }
}
int sum(int i)
{
    int s=0;
    while(i>0)
    {
        s+=c[i];
        i-=lowbit(i);
    }
    return s;
}
int main()
{
    while(scanf("%d%d%d",&n,&x,&y)==3)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].v);
            a[i].id=i;
        }
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        //离散化
        sort(a+1,a+n+1,cmp);
        //将最小的编号为1
        b[a[1].id]=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i].v!=a[i-1].v)
                b[a[i].id]=i;
            else
                b[a[i].id]=b[a[i-1].id];
        }
        ll ans=0;
        //这里用的很好
        //一开始c数组都是0,然后逐渐在b[i]处加上1;
        for(int i=1;i<=n;i++)
        {
            add(b[i],1);
            ans+=sum(n)-sum(b[i]);
        }
        if(x<y)
            swap(x,y);
        printf("%lld\n",y*ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81218435