Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14939 Accepted Submission(s): 9129
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/*题意: 已知一个数列,数列个数n<=5000,数列中的数的范围在[0,n-1],并且数不重复。 每次可以将数列的第一个数移动到最后面,这样可以构造出n个数列。求这n个数列中的 最小逆序数。(求一个数列的逆序对方法就不说了,想了解的话看我上一篇博客) 思路: 我们求出第一个数列的逆序数后,没有必要再去进行同样操作去求其他数列的逆序对 (第一次这样做超时)。其实只要计算出第一个数列的逆序数,其他数列的逆序数可以用这个已 经计算好的逆序数推出。具体的做法是:当一个数a移动到最后面时,那么这个数后面会有n-a-1 个数比它大,a个比它小,移动后,这个数的逆序数会增加n-a-1,其它数的逆序数会减小a,所以对 当前序列来说逆序数会增加(n - 1 - a - a)。因此只需要n次遍历便可求出其他数列的逆序对数 目,然后每次更新即可。*/ #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; #define MIN(a,b) a>b?b:a #define INF 0x3f3f3f3f int a[5100*2],cnt; int b[5100]; int c[5100]; void merge(int a[],int s,int m,int e) { int i=s,j=m+1,k=s; while(i<=m&&j<=e) { if(a[i]<=a[j]) b[k++]=a[i++]; else { cnt+=j-k; b[k++]=a[j++]; } } while(i<=m) b[k++]=a[i++]; while(j<=e) b[k++]=a[j++]; for(i=s;i<=e;i++) a[i]=b[i]; } void mergesort(int a[],int s,int e) { if(s<e) { int m=(s+e)/2; mergesort(a,s,m); mergesort(a,m+1,e); merge(a,s,m,e); } } int main() { int n,i,j,m,min,t; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%d",&a[i]); c[i]=a[i]; } cnt=0; mergesort(a,0,n-1); min=cnt; for(i=0;i<n;i++) { min+=n-1-c[i]-c[i]; cnt=MIN(cnt,min); } printf("%d\n",cnt); } return 0; }
//这个是第一次写的超时。。。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; #define MIN(a,b) a>b?b:a #define INF 0x3f3f3f3f int a[5100],cnt; int b[5100]; int c[5100]; void merge(int a[],int s,int m,int e) { int i=s,j=m+1,k=s; while(i<=m&&j<=e) { if(a[i]<=a[j]) b[k++]=a[i++]; else { cnt+=j-k; b[k++]=a[j++]; } } while(i<=m) b[k++]=a[i++]; while(j<=e) b[k++]=a[j++]; for(i=s;i<=e;i++) a[i]=b[i]; } void mergesort(int a[],int s,int e) { if(s<e) { int m=(s+e)/2; mergesort(a,s,m); mergesort(a,m+1,e); merge(a,s,m,e); } } int main() { int n,i,j,m,min,t; while(scanf("%d",&n)!=EOF) { min=INF; for(i=0;i<n;i++) scanf("%d",&a[i]); for(t=0,i=0;i<n;i++) { memset(c,0,sizeof(c)); cnt=0;t=0; for(j=i;j<n;j++) c[t++]=a[j]; for(j=0;j<i;j++) c[t++]=a[j]; /*for(j=0;j<t;j++) printf("%d ",c[j]); printf("\n");*/ mergesort(c,0,n-1); min=MIN(min,cnt); } printf("%d\n",min); } return 0; }