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);
}
}
}