Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6529 Accepted Submission(s): 3979
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.
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#include<iostream> using namespace std; #define MAX 5000 typedef struct infor { int left , right , value ; }infor ; infor tree[MAX*3]; void create(int root, int left , int right ) { tree[root].left = left ; tree[root].right = right ; tree[root].value = 0 ; if(right == left ) return ; int mid = (left + right ) / 2 ; create(root*2 , left , mid); create(root*2 + 1 , mid + 1 , right ); } void update(int root, int pos) { tree[root].value += 1; if(tree[root].left == tree[root].right ) return ; int mid = (tree[root].left + tree[root].right ) / 2 ; if(pos > mid ) update(root*2 + 1, pos); else update(root*2 , pos); } int cal(int root, int left , int right ) { if(tree[root].left == left && tree[root].right == right ) return tree[root].value ; int mid = (tree[root].left + tree[root].right ) / 2; if(left > mid ) return cal(root*2 + 1 , left , right ); else if(right <= mid) return cal(root*2 , left , right ); else return cal(root*2 + 1 , mid + 1, right ) + cal(root*2 , left , mid ); } int main() { int n; while(cin >> n) { int i ,a[MAX+1]; create(1,0,n-1); int ans = 0 ; for( i = 0 ; i < n ; i ++){ cin >> a[i] ; ans+=cal(1,a[i],n-1); update(1,a[i]); } int min = ans ; for(i = 0 ; i < n ; i ++) { ans -=a[i]; ans +=n-a[i] - 1; if(ans<min) min=ans; } cout<<min<<endl; } return 0; }
#include<iostream> using namespace std; #define MAX 5000 int a[3*MAX],c[3*MAX] , n ; int lowbit( int k ) { return k&(-k) ; } void update(int sum , int k) { while( k <= n ) { c[k]+=sum ; k+=lowbit(k); } } int cal( int k) { int sum = 0 ; while(k > 0 ) { sum += c[k]; k-=lowbit(k); } return sum ; } int main( void ) { while( cin >> n ) { memset(c,0,sizeof(c)); int i; int ans=0; for( i = 1 ; i <= n ; i ++) { cin >> a[i] ; a[i]++; ans+=cal(n)-cal(a[i]) ; update( 1 , a[i]); } int sum = ans ; for( i =1 ; i <= n ; i ++) { ans+=n-a[i]-(a[i]-1) ; if(sum>ans) sum = ans ; } cout<< sum << endl ; } return 0 ; }