Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11249 Accepted Submission(s): 6912
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
思路:先用树状数组求出当前序列的对数,然后ret=ret-(num[i]-1)+(n-num[i]);线段树也可以。
树状数组AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,tree[5010],num[5010];
int lowbit(int x)
{
return x&(-x);
}
void update(int pos)
{
for(;pos<=n;pos+=lowbit(pos))
tree[pos]++;
}
int query(int pos)
{
int ret=0;
for(;pos>0;pos-=lowbit(pos))
ret+=tree[pos];
return ret;
}
int main()
{
int i,j,k,ans,ret;
while(~scanf("%d",&n))
{
memset(tree,0,sizeof(tree));
memset(num,0,sizeof(num));
ret=0;
for(i=1;i<=n;i++)
{
scanf("%d",&k);
k++;
update(k);
num[i]=k;
ret+=i-1-query(k-1);
}
ans=ret;
for(i=1;i<=n;i++)
{
ret=ret-(num[i]-1)+(n-num[i]);
ans=min(ans,ret);
}
printf("%d\n",ans);
}
}
线段树AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int num,l,r;
}tree[20010];
int val[5010];
void build(int o,int l,int r)
{
tree[o].l=l;
tree[o].r=r;
tree[o].num=0;
if(l==r)
return;
int mi=(l+r)>>1;
build(o<<1,l,mi);
build(o<<1|1,mi+1,r);
}
void update(int o,int pos)
{
if(tree[o].l==pos && tree[o].r==pos)
{
tree[o].num=1;
return;
}
int mi=(tree[o].l+tree[o].r)>>1;
if(pos<=mi)
update(o<<1,pos);
else
update(o<<1|1,pos);
tree[o].num=tree[o<<1].num+tree[o<<1|1].num;
}
int query(int o,int l,int r)
{
if(tree[o].l==l && tree[o].r==r)
return tree[o].num;
int mi=(tree[o].l+tree[o].r)>>1;
if(r<=mi)
return query(o<<1,l,r);
if(l>mi)
return query(o<<1|1,l,r);
return query(o<<1,l,mi)+query(o<<1|1,mi+1,r);
}
int main()
{
int n,i,j,k,ret,ans;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&val[i]);
val[i]++;
}
build(1,1,n);
ret=0;
for(i=1;i<=n;i++)
{
ret+=query(1,val[i],n);
//printf("%d\n",ret);
update(1,val[i]);
}
ans=ret;
//printf("%d\n",ret);
for(i=1;i<=n;i++)
{
ret=ret-(val[i]-1)+(n-val[i]);
//printf("%d\n",ret);
ans=min(ans,ret);
}
printf("%d\n",ans);
}
}

本文探讨了如何通过树状数组或线段树算法解决最小逆序数问题,即找到一系列数字排列中逆序对数量最少的情况。文章提供了完整的代码实现,并解释了核心算法思想。
2902

被折叠的 条评论
为什么被折叠?



