描述
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.
输入
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.
输出
For each case, output the minimum inversion number on a single line.
样例输入
10
1 3 6 9 0 8 5 7 4 2
样例输出
16
解题思路:用归并排序先求出原来序列的逆对数,循环从0到n-1,把前面的n个元素移动到后面,
因为元素的大小是从0到n-1,所以移动到最后逆序对减少了a[i]个,增加了(n-1-a[i])个,记录最小的就行了、
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; #define Max 5000 int a[Max+5],temp[Max+5],c[Max+5]; long long sum; void memrge(int left,int mid,int right) { int i=left,j=mid+1,k=left; while(i<=mid&&j<=right) { if(a[i]>a[j]) { temp[k++]=a[j++]; sum+=mid-i+1; } else temp[k++]=a[i++]; } while(i<=mid) temp[k++]=a[i++]; while(j<=right) temp[k++]=a[j++]; for(int i=left;i<=right;i++) a[i]=temp[i]; } int fen(int left,int right) { if(left<right) { int mid=(left+right)/2; fen(left,mid); fen(mid+1,right); memrge(left,mid,right); } } int main() { int n,m,k; while(cin>>n) { sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); c[i]=a[i]; } fen(0,n-1); int Min=sum; for(int i=0;i<n;i++) { sum=sum-c[i]+(n-1-c[i]); if(sum<Min) Min=sum; } //将元素移到最后 cout<<Min<<endl; } return 0; }
原文地址:https://www.cnblogs.com/ww123/p/9073655.html