hdu 1394 解题报告 Minimum Inversion Number

Minimum Inversion Number

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

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
又是一个经典的线段树,首先要知道如何求一个序列的逆序——
比如2514 : 
2之前没有数大于它,所以为0,
5之前也没有数大于它,所以为0, 1之前2,5都大于它,所以为2, 4之前只有5大于它,所以为1,
因此2514的逆序数为:0  + 0 + 2 + 1 = 3;  从前面的描述中,我们可以发现,只要依次累计前面有几个数大于当前数即可。于是我们可以用一个数组l[n](初始化为0),然后每读取一个数x[i] 就标识l[x[i]] += 1 , 但是在标识之前我们先要统计有多少个数大于x[i],即累计l[x[i]+1] + ... +l[n-1],很显然累计的时间复杂度为O(N),我们能不能在快点呢?  
那么如何求最小逆序数呢?(我这里假设一个序列中每个数字都不同)  若abcde...的逆序数为sum,那么bcde...a的逆序数是多少?我们假设abcde...中小于a的个数为x[i] , 那么大于a的个数就是n - x[i],当把a移动左移一位时,原来比a大的现在都成了a的逆序对,故减少了x[i]+1,即 相比而言总共加了n-x[i]-x[i]-1;其实,线段树只有开始用了一次,后面求最小值就没用了,所以用暴力应该也能过,sum再更新N—-1次就可以得到最小值了,不算复杂!

于是我

#include <iostream>

#include<stdio.h>

using namespace std;

#define N 50000

int l[N<<2],x[N];

void build(int num ,int s,int e)

{

      l[num]=0;

      if(s==e)

            return ;

      int mid=(s+e)>>1;

      build(num<<1,s,mid);

      build(num<<1|1,mid+1,e);



}

int query(int num ,int s,int e ,int a,int b)

{

      if(a<=s&&b>=e)

            return l[num];

      int mid=(s+e)>>1;

      int ret=0;

      if(mid>=a)

            ret+=query(num<<1,s,mid,a,b);

      if(mid<b)

            ret+=query(num<<1|1,mid+1,e,a,b);

      return ret;

}

void update(int num ,int s,int e,int a)

{

      if(s==e)

      {

           l[num]++;

           return ;

      }

      int mid=(s+e)>>1;

      if(a<=mid)

            update(num<<1,s,mid,a);

      else

            update(num<<1|1,mid+1,e,a);

      l[num]=l[num<<1]+l[num<<1|1];

}

int main()

{

    int n,i;

    while(scanf("%d",&n)!=EOF)

    {

          build(1,0,n-1);

          int sum=0;

         for(i=0;i<n;i++)

         {

               scanf("%d",&x[i]);

               sum=sum+query(1,0,n-1,x[i],n-1);

               update(1,0,n-1,x[i]);

         }

         int re=sum;

         for(i=0;i<n-1;i++)

         {

               sum+=n-x[i]-x[i]-1;

               if(sum<re)

                  re=sum;

         }

         printf("%d\n",re);



    }



    return 0;

}



你可能感兴趣的:(version)