Minimum Inversion Number (线段树)

The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. 

For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following: 

a1, a2, ..., an-1, an (where m = 0 - the initial seqence) 
a2, a3, ..., an, a1 (where m = 1) 
a3, a4, ..., an, a1, a2 (where m = 2) 
... 
an, a1, a2, ..., an-1 (where m = n-1) 

You are asked to write a program to find the minimum inversion number out of the above sequences. 

Input

The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1. 

Output

For each case, output the minimum inversion number on a single line. 

Sample Input

10
1 3 6 9 0 8 5 7 4 2

Sample Output

16

数据较小,可以暴力:

#include <iostream>
using namespace std;
const int MAX = 5e3 + 5;
int num[MAX];
int main()
{
    ios::sync_with_stdio(false);
    int N;
    while (cin >> N){
        for (int i = 1; i <= N; i++){
        cin >> num[i];
    }
    int sum = 0;
    for (int i = 1; i <= N; i++){
        for (int j = 1; j < i; j++){
            if(num[j] > num[i]){
                sum ++;
            }
        }
    }
    int min = sum;
    for (int i = 1; i < N; i++){
        sum = sum - num[i] + (N - num[i] - 1);
        if (min > sum){
            min = sum;
        }
    }
    cout << min << endl;
    }
    return 0;
}

线段树:

#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 2e5 +5;
int num[MAX], st[MAX << 2];
void PushUp(int rt){
    st[rt] = st[rt << 1] + st[rt << 1 | 1];
}
void Build (int l, int r, int rt){
    st[rt] = 0;
    if (l == r){
        return ;
    }
    int m = (l + r) >> 1;
    Build(l, m, rt << 1);
    Build(m + 1, r, rt << 1 | 1);
}
void Update(int p, int l, int r, int rt){
    if (l == r){
        st[rt]++;
        return ;
    }
    int m = (l + r) >> 1;
    if (p <= m){
        Update(p, l, m, rt << 1);
    }else {
        Update(p, m + 1, r, rt << 1 | 1);
    }
    PushUp(rt);
}
int Query (int L, int R, int l, int r, int rt){
    if (L <= l && r <= R){
        return st[rt];
    }
    int m = (l + r) >> 1;
    int res = 0;
    if (L <= m){
        res += Query(L, R, l, m, rt << 1);
    }
    if (m < R){
        res += Query(L, R, m + 1, r, rt << 1 | 1);
    }
    return res;       //这里不是pushup()
}
int main()
{
    ios::sync_with_stdio(false);
    int N;
    while (cin >> N){
        Build(0, N - 1, 1);
        int res = 0;
        for (int i = 0; i < N; i++){
            cin >> num[i];
            res += Query(num[i], N - 1, 0, N - 1, 1);    //11
            Update(num[i], 0, N - 1, 1);                 //22  这两行的顺序
        }
        int Min = res;
        for (int i = 0; i < N; i++){
            res = res - num[i] + (N - num [i] - 1);
            Min = min(Min, res);
        }
        cout << Min << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43678290/article/details/88141511