Minimum Inversion Number HDU - 1394

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40758751/article/details/82380196

Minimum Inversion Number

HDU - 1394

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

AC代码

//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
//#define sd(x,y,z) scanf("%d%d%d", &x, &y, &z)
//#define sd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
//#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
#define MOD 1000000007
const int maxn = 5100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

int n, m, t, _n;
int dat[maxn<<2];
int d[maxn];
int vec[maxn];
void init(int _n)
{
    n = 1;
    while(n < _n) n<<=1;
    memset(d, 0, sizeof d);
    for(int i = 0; i < n*2-1; i++) dat[i] = 0;
}
void update(int k, int v)
{
    k += n-1;
    dat[k] = v;
    while(k>0)
    {
        k = (k-1)/2;
        dat[k] = dat[k*2+1] + dat[k*2+2];
    }
}
int query(int a, int b, int k, int l, int r)
{
    if(r<=a || b<=l) return 0;

    if(a<=l && r<=b) return dat[k];
    else
    {
        int vl = query(a, b, k*2+1, l, (l+r)/2);
        int vr = query(a, b, k*2+2, (l+r)/2, r);
        return vl+vr;
    }
}
int main()
{
	//freopen("in.txt", "r", stdin);
	while(~scanf("%d", &_n))
    {
        init(_n);
        int sum = 0;
        int ans = INF;
        for(int i = 0; i < _n; i++)
        {
            scanf("%d", &vec[i]);
            d[i] += query(vec[i]+1, _n, 0, 0, n);
            update(vec[i], 1);
            sum += d[i];
        }
        ans = sum;
        for(int i = 0; i < _n-1; i++)
        {
            int k = (_n-1)-vec[i];
            sum = sum  - vec[i] + k;
            if(sum < ans) ans = sum;
        }
        printf("%d\n", ans);
    }

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/82380196