Minimum inversion number
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10477 accepted submission (s): 6464
Problem descriptionthe 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.
Inputthe 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.
Outputfor each case, output the minimum inversion number on a single line.
Sample input101 3 6 9 0 8 5 7 4 2
Sample output16
Authorchen, Gaoli
Sourcezoj monthly, January 2003
Recommendignatius. l The meaning of the question is to give you a sequence, find the logarithm of the reverse Number of the sequence, and then put the first element at the end to generate a new sequence, it stops until the last element reaches the first, and calculates the logarithm of the smallest number of reverse orders in the generated sequence. Let me talk about my practices. 1. brute force: Find the logarithm of the Reverse Order Number of the new sequence each time, and then check whether it is the minimum value. Finally, output the minimum value. But here, the key technique is to find the logarithm of the Reverse Order Number of the new sequence, there is no need to scan the results again. You can think about it, because the number of the sequence ranges from 0 ~ N-1 consecutive, when the first number is moved to the end, the number larger than this number turns into a reverse number, A number smaller than this number is not a reverse number. For example, 3 2 4 5 originally 3 2 is a reverse number, and 3 4 5 is not a reverse number, when the sequence is changed to 2 4 5 3, the original 3 4 is changed to 4 3, which is the number of reverse orders, 2 3 is not a reverse order number, so the number smaller than the first element in the original sequence is (that is, the logarithm of the reverse order number) low [A [I] = A [I], so the number of numbers larger than the first element can be introduced as up [A [I] = n-1-a [I], then the logarithm of the inverse number in the new sequence is sum = sum- low [A [I] + up [A [I] = sum-A [I] + (n-1-a [I]) brute force code
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 int a[5005]; 7 int main() 8 { 9 int n,i,j;10 while(scanf("%d",&n)!=EOF)11 {12 int sum=0;13 for(i=0;i<n;i++)14 {15 scanf("%d",&a[i]);16 for(j=0;j<i;j++)17 if(a[j]>a[i])18 sum++;19 }20 int ans=sum;21 for(i=0;i<n;i++)22 {23 sum=sum-a[i]+(n-a[i]-1);24 if(sum<ans)25 ans=sum;26 }27 printf("%d\n",ans);28 }29 return 0;30 }
View code
2. Line Segment tree
In the same way, we also need to find the logarithm of the number in the reverse order. The brute force method is to scan the number from the first number to the current number. The method of the Line Segment tree is to use the line segment tree to query, which will be faster.
The latter is the same when processing the reverse logarithm of the new sequence.
Code of the Line Segment tree
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int l,r; 9 int num;10 int mid()11 {12 return (l+r)>>1;13 }14 }a[5000*4];15 int b[5005];16 void btree(int l,int r,int step)17 {18 a[step].l=l;19 a[step].r=r;20 a[step].num=0;21 if(l==r) return ;22 int mid=a[step].mid();23 btree(l,mid,step*2);24 btree(mid+1,r,step*2+1);25 }26 27 void ptree(int step,int vis)28 {29 a[step].num++;30 if(a[step].l==a[step].r) return ;31 int mid=a[step].mid();32 if(vis>mid)33 ptree(step*2+1,vis);34 else35 ptree(step*2,vis);36 }37 38 int fintree(int step,int x,int y)39 {40 if(a[step].l==x&&a[step].r==y)41 return a[step].num;42 int mid=a[step].mid();43 if(x>mid)44 return fintree(step*2+1,x,y);45 else if(y<=mid)46 return fintree(step*2,x,y);47 else48 return fintree(step*2,x,mid)+fintree(step*2+1,mid+1,y);49 }50 int main()51 {52 int n,i,j;53 while(scanf("%d",&n)!=EOF)54 {55 int ans=0;56 btree(0,n-1,1);57 for(i=0;i<n;i++)58 {59 scanf("%d",&b[i]);60 ans+=fintree(1,b[i],n-1);61 ptree(1,b[i]);62 }63 int minn=ans;64 for(i=0;i<n;i++)65 {66 ans=ans-b[i]+(n-1-b[i]);67 if(minn>ans) minn=ans;68 }69 printf("%d\n",minn);70 }71 return 0;72 }
View code