Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13722 Accepted Submission(s): 8371
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<cstdio>
#include<cstdlib>
#include<cstring>
#define Max 5010
using namespace std;
int array[Max];
int temp[Max];
int count,n;
void Merge_sort(int l,int mid,int r)
{
int i=l;
int j=mid+1;
int k=l;
while(i<=mid&&j<=r)
{
if(array[i]>array[j])
{
temp[k++]=array[j++];
count+=mid-i+1;
}
else
{
temp[k++]=array[i++];
}
}
while(i<=mid)temp[k++]=array[i++];
while(j<=r)temp[k++]=array[j++];
for(i=l;i<=r;++i)
{
array[i]=temp[i];
}
}
void Merge(int l,int r)
{
if(l<r)
{
int mid=(l+r)>>1;
Merge(l,mid);
Merge(mid+1,r);
Merge_sort(l,mid,r);
}
}
int Min(int a,int b)
{
return a>b?b:a;
}
int a[Max];
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i)
{
scanf("%d",&array[i]);
a[i]=array[i];
}
count=0;
Merge(0,n-1);
int ans=count;
for(i=0;i<n;++i)
{
ans+=n-2*a[i]-1;
count=Min(ans,count);
}
printf("%d\n",count);
}
return 0;
}
树状数组
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define Max 5010
#define inf 0x3f3f3f3f
using namespace std;
int c[Max];
int n;
int a[Max];
int Min(int a,int b)
{
return a>b?b:a;
}
int low(int n)
{
return n&(-n);
}
struct Node{
int t;
int pos;
}A[Max];
bool cmp(Node a,Node b)
{
if(a.t==b.t)
return a.pos>b.pos;
return a.t>b.t;
}
void update(int t)
{
while(t<=n)
{
c[t]+=1;
t+=low(t);
}
}
int sum(int t)
{
int sum=0;
while(t>0)
{
sum+=c[t];
t-=low(t);
}
return sum;
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i)
{
scanf("%d",&A[i].t);
a[i]=A[i].t;
A[i].pos=i+1;c[i]=0;
}
stable_sort(A,A+n,cmp);
int count=0,ans=0;
for(i=0;i<n;++i)
{
update(A[i].pos);
if(A[i].pos==1)continue;
ans+=sum(A[i].pos-1);
}
count=ans;
for(i=0;i<n;++i)
{
count+=n-2*a[i]-1;
ans=Min(ans,count);
}
printf("%d\n",ans);
}
return 0;
}