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 <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define maxn 5008
int A[maxn];//用来存元素
inline int min(int a,int b)
{
return a>b?b:a;
}
struct ST
{
int l,r,key;//一段的元素个数和
}st[4*maxn];
void buildtree(int id,int l,int r)
{
st[id].l=l;
st[id].r=r;
if(l==r)
{
st[id].key=0;
return;
}
int mid=(l+r)>>1;
buildtree(2*id,l,mid);
buildtree(2*id+1,mid+1,r);
}
void update(int id,int udid)
{
if(st[id].l==udid&&st[id].r==udid)
{
st[id].key++;
return;
}
if(st[2*id].r>=udid)
{
update(2*id,udid);
st[id].key=st[2*id].key+st[2*id+1].key;
return;
}
if(st[2*id+1].l<=udid)
{
update(2*id+1,udid);
st[id].key=st[2*id].key+st[2*id+1].key;
return;
}
}
int query(int id,int l,int r)
{
if(st[id].l==l&&st[id].r==r)
{
return st[id].key;
}
if(st[2*id].r>=r)
{
return query(2*id,l,r);
}
if(st[2*id+1].l<=l)
{
return query(2*id+1,l,r);
}
return query(2*id,l,st[2*id].r)+query(2*id+1,st[2*id+1].l,r);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
buildtree(1,1,n);
int sum=0;//用来存初始的逆序数
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
update(1,A[i]+1);//我把所给的数全部+1了
if(A[i]+1!=n)
sum+=query(1,A[i]+2,n);//找出比他大的数的个数
}
//初始逆序数求出来了,接下来要根据已知的逆序数递推出
int ans=sum;
for(int i=2;i<=n;i++)//A[i]做第一个数
{
if(A[i-1]+1==n)//如果前一个数是n
{
ans=ans-query(1,1,n-1);
}
else if(A[i-1]+1==1)//如果前一个数是1
{
ans=ans+query(1,2,n);
}
else
{
ans=ans-query(1,1,A[i-1])+query(1,A[i-1]+2,n);
}
sum=min(sum,ans);
}
printf("%d\n",sum);
}
return 0;
}
代码风格更新后:
/*
用个sum来存这一段内的数的个数
*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
#define maxn 5008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
int nixu,num[maxn];
struct ST
{
int l,r,sum;
}st[4*maxn];
void PushUp(int id)
{
st[id].sum=st[2*id].sum+st[2*id+1].sum;
}
void buildtree(int id,int l,int r)
{
st[id].l=l;
st[id].r=r;
if(l==r)
{
st[id].sum=0;
return;
}
int mid=(l+r)>>1;
buildtree(lson);
buildtree(rson);
st[id].sum=0;
}
int query(int id,int l,int r)
{
if(st[id].l==l && st[id].r==r)
{
return st[id].sum;
}
if(st[2*id].r>=r)
{
return query(2*id,l,r);
}
if(st[2*id+1].l<=l)
{
return query(2*id+1,l,r);
}
return query(2*id,l,st[2*id].r)+query(2*id+1,st[2*id+1].l,r);
}
void update(int id,int udid)
{
if(st[id].l==udid && st[id].r==udid)
{
st[id].sum++;
return;
}
if(st[2*id].r>=udid)
{
update(2*id,udid);
PushUp(id);
return;
}
if(st[2*id+1].l<=udid)
{
update(2*id+1,udid);
PushUp(id);
return;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
nixu=0;
buildtree(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
update(1,num[i]+1);
if(num[i]!=n-1)nixu+=query(1,num[i]+2,n);
}
int lnixu=nixu;//上一次的逆序
for(int i=1;i<n;i++)
{
int add=0;
if(num[i]==0)
{
add+=query(1,2,n);
}
else if(num[i]==n-1)
{
add-=query(1,1,n-1);
}
else
{
add+=query(1,num[i]+2,n)-query(1,1,num[i]);
}
lnixu+=add;
if(lnixu<nixu)nixu=lnixu;
}
printf("%d\n",nixu);
}
return 0;
}