Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12868 Accepted Submission(s): 7860
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
Author
CHEN, Gaoli
Source
Recommend
这道题的关键是怎样求出每一个序列的逆序数,观察题目中描述的序列产生方法,可以看出每次都是将第一个元素移动到末尾,而且数字是从0到n-1且不重复,也就是说,这个序列必定是由0到n-1这n个数组成。
考虑将第一个数移动到序列末尾这个过程,因为逆序对的定义是前面的元素大于后面的元素,所以当a1在头上的时候,一定是比它小的元素可以和它组成逆序对,一共有a1对;而移动到末尾之后,只有比它大的元素才能和它组成逆序对,共有n-1-a1对。
也就是说,每次将a1移动到末尾之后,会减少a1对逆序对,增加n-1-a1对逆序对,总共增加n-1-2*a1个,这是一个递推关系,所以可以通过循环n-1次实现。
现在问题又来了,怎样求初始序列的逆序数,方法大体有三种,线段树,树状数组和归并排序,前两种都是位置相关的,也就是说是把每一个元素当成位置来处理,这里简单介绍一下如何用线段树处理:
1.首先建树,每个节点保存这个区间内的数字的个数。
2.每次输入一个数字a,查找从a到n-1这个区间的数字的个数,如果这个区间内有数字,意味着有比a大的数字在a之前,有几个数逆序对的个数就是几。
3.将数字a放到编号为3的节点,并且更新所有节点的数值。
4.最后算出的sum就是一个序列的逆序数。
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 20000
using namespace std;
int tree[MAX];
void PushUp(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void Creat(int l,int r,int rt)
{
tree[rt]=0;
if(l==r)
{
return;
}
int m=(l+r)>>1;
Creat(l,m,rt<<1);
Creat(m+1,r,rt<<1|1);
}
void Update(int p,int l,int r,int rt)
{
if(l==r)
{
tree[rt]++;
return;
}
int m=(l+r)>>1;
if(p<=m)
Update(p,l,m,rt<<1);
else
Update(p,m+1,r,rt<<1|1);
PushUp(rt);
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return tree[rt];
}
int m=(l+r)>>1;
int ans=0;
if(L<=m)
ans+=Query(L,R,l,m,rt<<1);
if(R>m)
ans+=Query(L,R,m+1,r,rt<<1|1);
return ans;
}
int main()
{
int n,num[5050];
int i;
int sum;
int Min;
while(~scanf("%d",&n))
{
sum=0;
Creat(0,n-1,1);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);//输入数字
sum+=Query(num[i],n-1,0,n-1,1);//查找在num之前并且大于它的数字的个数
Update(num[i],0,n-1,1);//将这个数字插入到线段树中并且更新
}
Min=sum;
for(i=0;i<n;i++)//迭代求逆序数
{
sum+=n-2*num[i]-1;
Min=min(Min,sum);
}
if(Min<0)
Min=0;
cout<<Min<<endl;
}
return 0;
}
下面附上通过归并排序模板做的,注意归并排序本身需要两个数组,并且这两个数组都会发生改变,所以必须是临时的。
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 20000
using namespace std;
int a[5050];
int c[5050];
int num[5050];
int cnt;
void MergeSort(int l,int r)
{
int mid,i,j,tmp;
if(r>l+1)
{
mid=(l+r)/2;
MergeSort(l,mid);
MergeSort(mid,r);
tmp=l;
for(i=l,j=mid;i<mid&&j<r;)
{
if(a[i]>a[j])
{
c[tmp++]=a[j++];
cnt+=mid-i;
}
else
c[tmp++]=a[i++];
}
if(j<r)
for(;j<r;j++)
c[tmp++]=a[j];
else
for(;i<mid;i++)
c[tmp++]=a[i];
for(i=l;i<r;i++)
a[i]=c[i];
}
}
int main()
{
int n;
int i;
//int sum;
int Min;
while(~scanf("%d",&n))
{
cnt=0;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);//输入数字
a[i]=num[i];
}
MergeSort(0,n);
Min=cnt;
for(i=0;i<n;i++)//迭代求逆序数
{
cnt+=n-2*num[i]-1;
Min=min(Min,cnt);
}
if(Min<0)
Min=0;
cout<<Min<<endl;
}
return 0;
}