HDU 1394 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

题意:

求反转后的按照上面操作后形成的序列中最小的逆序数。

逆序数是什么呢? 例如:4 2 3 1,这个序列的逆序数为:

(4,2), (4,3) ,(4,1) ,(2,1), (3,1) 。

也就是符合i<j,但是a[i]>a[j].

思路:

这道题我想了好久也没想出咋做, 无奈只好看题解。 。。

之后恍然大悟。

可以通过求出初始的逆序数, 然后通过规律求出其他形式的数组的逆序数。

先说如何求初始逆序数。 可以通过线段树的性质, 建一个[1,n]的线段树, 初始化为0, 然后将输入的数字依次放入线段树中, 然后求区间和。 这样就求出逆序数了。

然后是转换。 接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;(以上来自其他博客上大佬的分析)。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=5005;
int tree[maxn<<2];
int a[maxn];
int n;
void pushup (int re)
{
    tree[re]=tree[re<<1]+tree[re<<1|1];
}
void build (int l,int r,int re)
{
    if(l==r)
    {
        tree[re]=0;
        return ;
    }
    int mid=(l+r)>>1;
    build (l,mid,re<<1);
    build (mid+1,r,re<<1|1);
    pushup (re);
}
void update (int loc,int l,int r,int re)
{
    if(l==r)
    {
        tree[re]++;
        return;
    }
    int mid=(l+r)>>1;
    if(loc<=mid)
        update (loc,l,mid,re<<1);
    else
        update (loc,mid+1,r,re<<1|1);
    pushup (re);
}
int query (int left,int right,int l,int r,int re)
{
    if(l>=left&&r<=right)
           return tree[re];
    int mid=(l+r)>>1;
    int ans=0;
    if(mid>=left)
        ans+=query (left,right,l,mid,re<<1);
    if(mid<right)
        ans+=query (left,right,mid+1,r,re<<1|1);
    return ans;
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        //memset (tree,0,sizeof(tree));
        int sum=0;
        build (1,n+1,1);
        for (int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=query (a[i]+2,n+1,1,n+1,1);
            //printf("%d\n",sum);
            update (a[i]+1,1,n+1,1);
        }
       // printf("%d\n",sum);
        int ans=sum;
        for (int i=0;i<n;i++)
        {
            ans=ans-2*a[i]+n-1;
            sum=min(sum,ans);
        }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81613033