HDU 1394 Minimum Inversion Number(线段树:单点更新,求逆序数)

http://acm.hdu.edu.cn/showproblem.php?pid=1394



Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14648    Accepted Submission(s): 8942


Problem Description
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


   题意:给你0到n-1的随机序列,求出n种顺序变换后最小的逆序数。比如2341逆序数:2的逆序数为1,所以n=1,3的逆序数为1,所以n=1+1=2,4的逆序数为1,所以n=2+1=3,1的逆序数为0,所以2341的逆序数为n=3; 每次把第一位数放到后面去,这时3412的逆序数为4,4123的逆序数为4,1234的逆序数为0,所以所有情况下,序列最小的逆序数为n=0。


解题思路:这题暴力也会过的,而且很巧妙。但在这我描述下如何使用线段数求解。我看了好多博客后来才理解的。

                 利用线段树只需要计算出原始序列的逆序数,然后利用这个逆序数递推其他序列的逆序数。对于第i数 ,

                 他的逆序数等于已经插入到线段树中比它大的数的个数(query(1,a[i]+1,n-1)。

                

                  求出初始序列逆序数后: 

                           例如序列 2 0 3 1 4 5 的逆序数是3,把2放到最后边以后,比2小的数(2个)的每个数的逆序数减1,比2

                 大的数(n-a[i]-1个)的逆序数不变,而2的逆序数变为比2大的数的个数(n-a[i]-1)。根据这个结论,我们

                 就可以递推出其他序列的逆序数,进而求出最小值。



<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=5010;
int a[maxn];
int n,ans;
struct node{
   int left,right,sum;
}t[maxn<<2];
void build(int k,int l,int r)
{
    t[k].left=l;t[k].right=r;
    t[k].sum=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
}
void update(int k,int val)
{
    int l=t[k].left,r=t[k].right;
    if(l==val && val==r){
        t[k].sum++;
        return;
    }
    int mid=(l+r)>>1;
    if(val<=mid) update(k<<1,val);
    else update(k<<1|1,val);
    t[k].sum=t[k<<1].sum+t[k<<1|1].sum;
}
void query(int k,int l,int r)
{
    if(l==t[k].left && r==t[k].right)
    {
        ans+=t[k].sum;
        return;
    }
    int mid=(t[k].left+t[k].right)>>1;
    if(r<=mid) query(k<<1,l,r);
    else if(l>mid) query(k<<1|1,l,r);
    else{
        query(k<<1,l,mid);
        query(k<<1|1,mid+1,r);
    }
}
int main()
{
    while(scanf("%d",&n) != EOF)
    {
        build(1,0,n);//虽然只有到n-1,但是由于后面用到a[i]+1,为防止溢出,树区间要到n
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            query(1,a[i],n-1);//计算比a[i]大的数的个数。注:这些数是包含于插入a[i]前的
            update(1,a[i]);//此时将a[i]插入
        }
        int temp=ans;
        for(int i=0;i<n-2;i++)
        {
            ans=ans-a[i]+(n-a[i]-1);
            temp=min(temp,ans);
        }
        printf("%d\n",temp);
    }
    return 0;
}
</span>






你可能感兴趣的:(HDU 1394 Minimum Inversion Number(线段树:单点更新,求逆序数))