HDU1394 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

题意就是寻找不同序列的最小逆序数,把序列的第一个元素放在最后面,形成一个新序列,记录下该序列的逆序数,依次直到原序列中的最后一个元素成为第一个元素,求这些序列逆序数的最小值。

首先,感谢超大神的指导,嘿嘿嘿。。

基本思想:填数+逆序数原理。

逆序数,假如升序为正序列,那么大数在小数前面就是逆序,一种情况为一个逆序对,逆序对的数量就是逆序数。

例如 14325 逆序对4 3 ,4 2,逆序数为2
 

填数原理就是根据此产生。线段树中存储的是“已填数”的数量,即在某个范围内已经有几个数填入了

例如

10

1 3 6 9 0 8 5 7 4 2

换即

2 4 7 10 1 9 6 8 5 3

先填2 我们把线段树上2~2范围(即单点修改)修改为1,即已填过,代表该数已填过。前面没有填,也就是说1在2后面,一个逆序对产生

填4,。。。。。。。。。。,有个2已经填过了,还有1 3,逆序对4 1 ,4 3,两个逆序对。

这样就求出原序列对的逆序数

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

每次把第一个数放到最后边逆序对变化量:n-a[i]-(a[i]-1)

          为什么是这个:在第一个位置的数,比它小的有a[i]-1个,比它大的有n-a[i]个,所以放到最后边对原来序列逆序对的贡献值就是:n-a[i]-(a[i]-1)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int n;
	static int max=5005;
	static int[] tree=new int[max<<2];
	static int[] arr=new int[max<<2];
	static void update_tree(int node , int start , int end , int L , int R ){
		if(L==start&&R==end){
			tree[node]+=1;
			return;
		}
		int mid=(start+end)>>1;
		
		if(L<=mid){
			update_tree(node<<1,start,mid,L,R);
		}else{
			update_tree(node<<1|1,mid+1,end,L,R);
		}
		tree[node]=tree[node<<1]+tree[node<<1|1];
	}
	static int query_tree(int node,int start,int end,int L,int R){
		if(L<=start&&end<=R){
			return tree[node];
		}
		int mid=(start+end)>>1;
		if(R<=mid){
			return query_tree(node<<1,start,mid,L,R);
		}else if(L>mid){
			return query_tree(node<<1|1,mid+1,end,L,R);
		}else{
			return query_tree(node<<1,start,mid,L,R)+query_tree(node<<1|1,mid+1,end,L,R);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			n=sc.nextInt();
			Arrays.fill(tree,0);
			for(int i=1;i<=n;i++){
				arr[i]=sc.nextInt()+1;
			}
			int sum=0;
			for(int i=1;i<=n;i++){
				int a=arr[i];
				update_tree(1,1,n,a,a);
				sum+=i-query_tree(1,1,n,1,a);
			}
			int ans=sum;
			for(int i=1;i<n;i++){
				sum+=(n-arr[i])-(arr[i]-1);
				if(sum<ans)
					ans=sum;
			}
			System.out.println(ans);
		}
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值