Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8317 Accepted Submission(s): 5109
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
Ignatius.L
线段树,第一遍初始化的时候计算出逆序对,然后之后每次把第一个数移动到最后一个位置的时候需要把比他小的那部分数都减一,然后这个数的值就应该是比他大的数的个数,因为,移到最后一个位置上了所有数都在他前面。。。。
代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct
{
int l,r,val,lazy,show;
}Tree;
Tree tree[50000];
int a[5005];
void Push(int t)
{
tree[2*t+1].val+=(tree[2*t+1].r-tree[2*t+1].l+1)*tree[t].lazy;
tree[2*t+2].val+=(tree[2*t+2].r-tree[2*t+2].l+1)*tree[t].lazy;
tree[2*t+1].lazy+=tree[t].lazy;
tree[2*t+2].lazy+=tree[t].lazy;
tree[t].lazy=0;
}
void Build(int t,int l,int r)
{
tree[t].l=l;
tree[t].r=r;
tree[t].val=0;
tree[t].lazy=0;
tree[t].show=0;
if (l==r) return;
int mid=(l+r)>>1;
Build(2*t+1,l,mid);
Build(2*t+2,mid+1,r);
}
void point_update(int t,int x,int val)
{
if (tree[t].l==tree[t].r)
{
tree[t].val=val;
tree[t].show=1;
return;
}
if (tree[t].lazy!=0)
{
Push(t);
}
int mid=(tree[t].l+tree[t].r)>>1;
if (x<=mid) point_update(2*t+1,x,val);
else point_update(2*t+2,x,val);
tree[t].val=tree[2*t+1].val+tree[2*t+2].val;
tree[t].show=tree[2*t+1].show+tree[2*t+2].show;
}
void line_update(int t,int l,int r,int val)
{
if (l>r) return;
if (tree[t].l==l && tree[t].r==r)
{
tree[t].val+=(tree[t].r-tree[t].l+1)*val;
tree[t].lazy+=val;
return;
}
if (tree[t].lazy!=0)
{
Push(t);
}
int mid=(tree[t].l+tree[t].r)>>1;
if (l<=mid) line_update(2*t+1,l,min(mid,r),val);
if (r>mid) line_update(2*t+2,max(l,mid+1),r,val);
tree[t].val=tree[2*t+1].val+tree[2*t+2].val;
}
int query_show(int t,int l,int r)
{
if (tree[t].l==l && tree[t].r==r)
{
return tree[t].show;
}
int mid=(tree[t].l+tree[t].r)>>1;
int ret=0;
if (l<=mid) ret+=query_show(2*t+1,l,min(mid,r));
if (mid<r) ret+=query_show(2*t+2,max(mid+1,l),r);
return ret;
}
int main()
{
int i,j,n,ans;
while(scanf("%d",&n)!=EOF)
{
Build(0,0,n-1);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
point_update(0,a[i],query_show(0,a[i],n-1));
}
ans=tree[0].val;
for (i=0;i<n;i++)
{
point_update(0,a[i],n-a[i]-1);
line_update(0,0,a[i]-1,-1);
ans=min(ans,tree[0].val);
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一个算法用于计算给定整数序列中通过不同排列得到的序列的最小反转次数。通过线段树数据结构,实现高效的查找和更新操作,解决序列排列优化问题。
655





