Minimum Inversion Number

Minimum Inversion Number

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 33 Accepted Submission(s) : 24
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

Author
CHEN, Gaoli

Source
ZOJ Monthly, January 2003
其实就是求最小逆序数
//先说一下逆序数的概念:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那末它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为偶数的排列称为偶排列;逆序数为奇数的排列称为奇排列。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。思路:每次把末尾的数掉到序列前面时,减少的逆序对数为n-1-a[i] ,增加的逆序对数为a[i] ,这样就可在所有的序列中找出含有逆序对最少的了!
暴力型的AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 60000
int a[N];
int i,n,min;
int main()
{
	while(scanf("%d",&n)!=EOF)//不加EOF就会TLE;
	{
		int sum=0;
		memset(a,0,sizeof(a));
		for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
		for(i=1;i<n;i++)
		{
		   for(int j=i+1;j<=n;j++)
		   {
   			if(a[i]>a[j])
   			sum++;
   		   }	
		}
		   
                  int min=sum;  
	    for(i=n;i>=1;i--)
	    {
    		sum-=n-1-a[i];
    		sum+=a[i];
    	
    	if(sum<min)
    	
	    	min=sum;
	    }
    	
		printf("%d\n",min);
	}
	return 0;
} 

这个跟上面那个差不多:
//设num为逆序数,w[i]为第i个数字

//N种排列中选一种, 它的逆序数最小. 输出这个最小逆序数.

//现在的做法是先O(n^2)求出最初的逆序数, 然后不断地把最后一个数插到最前面, 再计算一下逆序数的变化. 具体的方法就是: num-=n-1-w[n-1]; num+=w[n-1]

//理由 : 把最后面的数字插入最前面, 所有原来的w[i](i<n-1且w[i]>w[n-1])就不再具有i<n-1且w[i]>w[n-1]的关系. 由题意可知道刚好有n-1-w[n-1]个. (数字是0~n-1). num+=w[n-1]同理

//每次改变排列就更新一下最小值,最后输出即可.

//计算逆序数这地方应该是可以优化的吧...暂时也没想到.

//代码:

#include <stdio.h>

const int maxn=5001;

int num[maxn];

int main(){
      int n;
      while(scanf("%d",&n)!=EOF)
	  	{
          int i;
          for(i=0;i< n;++i)
		  	{
              scanf("%d",&num[i]);
            }
          int sum=0;
          int min;
          int j;
          for(i=0;i< n;++i)
		  	{
              for(j=i+1;j< n;++j)
			  	{
                  sum+=(num[i]>num[j]);
                }
            }
          min=sum;
          for(i=n-1;i>0;--i)
		  	{
              sum-=n-1;//? 
              sum+=(num[i]<< 1);//靠,看见位运算就不爽 
			 
              if(sum< min)
			  	{
                  min=sum;
                }
            }
          printf("%d\n",min);
       }
      return 0;
}
 
用树状数组实现:
代码:

#include <stdio.h>
#include <string.h>
#define M 5001
int n;
int a[M], c[M];
int lowbit(int x)
{
 return x&(-x);
}

void update(int x, int num)
{
 while(x <= n)
 	{
     c[x] += num;
     x += lowbit(x);
    }
}

int getSum(int x)
{
 int sum = 0;
 while(x > 0)
 	{
      sum += c[x];
      x -= lowbit(x);
    }
 return sum;
}

int main()
{
   int i, j, k, total, min;
   while( scanf("%d", &n) != EOF )
   	{
  
        min = 15000000;
        memset(c, 0, sizeof(c));
        total = 0;
        for( i = 1; i <= n; ++i)
			{
               scanf("%d", &a[i]);
               ++a[i];
               update(a[i], 1);
               total += (i - getSum(a[i]));
            }
        min = total;
        for( i = 1; i < n; ++i)
		    { 
               //减去a[i]表示a[i]这个位置前面有多少个比a[i]小的数
               //加上(n - a[i] - 1)表示a[i]这个位置后面有多少个比a[i]大的数
               total = total - a[i] + 1  + (n - a[i]);
               min = total < min? total: min;
            }    
     printf("%d\n", min);
     }
 return 0;
}
 
      




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值