Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20737 Accepted Submission(s): 12434
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.
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
ZOJ Monthly, January 2003
题意:
给定一个长度为n的序列,问每次可以将序列的第一个数移到最后一个,它的逆序数最小是多少?
分析:首先可以分析得知把序列的首元素移到最后一个,其逆序数的变化为n-a[i]-1(增加的),a[i](减少的);假设未移动时的逆序数个数为sum,移动后的逆序数个数就等于sum+n-a[i]-1-a[i].
此题可以用线段树,后者树状数组求解,当然也可以用归并排序,在这儿不针对归并排序分析,此题的代码可以用树状数组写,而且不容易出错,能用树状数组的尽量用它写。
1.线段树
#include
#include
using namespace std;
const int N=5e3+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int sum[N<<2],a[N];///sum开4倍空间较为安全
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
sum[rt]=0;///初始值
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r)
{
sum[rt]++;
return;
}
int mid=(l+r)>>1;
if(p<=mid) update(p,lson);
if(p>mid) update(p,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int res=0;
int mid=(l+r)>>1;
if(L<=mid) res+=query(L,R,lson);
if(R>mid) res+=query(L,R,rson);
return res;
}
int main()
{
int n,sum=0;
while(~scanf("%d",&n))
{
sum=0;
build(1,n,1);///建立线段树
for(int i=0;i"%d",&a[i]);
sum+=query(a[i]+1,n,1,n,1);//对当前输入的数计算当前序列的逆序数
update(a[i],1,n,1);
}
int ans=sum;///此时sum为输入序列的总的逆序数
for(int i=0;isum+=n-a[i]-1-a[i];///如分析
ans=min(ans,sum);///求最小的
}
printf("%d\n",ans);
}
return 0;
}
2.树状数组
#include
#include
#include
#include
using namespace std;
const int N=5005;
int c[N],a[N],n;
int lowbit(int i) {
return i&(-i);
}
void update(int pos,int val) {
while(pos<=n) {
c[pos]+=val;
pos+=lowbit(pos);
}
}
int sum(int i) {
int s=0;
while(i>0) {
s+=c[i];
i-=lowbit(i);
}
return s;
}
int main() {
while(~scanf("%d",&n)) {
int res=0;
memset(c,0,sizeof(c));
for(int i=0; iscanf("%d",&a[i]);
a[i]++;
res+=sum(n)-sum(a[i]);///当前序列的逆序数对的个数
// printf("%-3d %-3d %-3d %-3d ",a[i],sum(n),sum(a[i]),res);
update(a[i],1);///修改BIT中Aj位置上的值
/*printf("%-3d\n",c[i]);
puts("-------------");
for(int i=1;i<=n;i++) printf("%d ",c[i]);
puts("");
puts("-------------");*/
}
int Min=res;
for(int i=0; i1); ///移动后的逆序数与移动前的逆序数对的关系,减去比之前小的,加上比之后大的
// printf("res=%d\n",res);
Min=min(Min,res);///取最小的逆序数对的个数
}
printf("%d\n",Min);
}
return 0;
}
明显这里用树状数组比较简洁方便,不容易出错。
3.归并排序
#include
#include
#include
#include
using namespace std;
const int mod=10056;
const int INF=0x3f3f3f3f;
const int N=5e3+5;
typedef long long LL;
#define mem(a,n) memset(a,n,sizeof(a))
int a[N],b[N],T[N];
int cnt=0;
void merge_sort(int *A,int x,int y)
{
if(y-x>1)
{
int m=(x+y)/2;
int l=x,i=x,r=m;
merge_sort(A,x,m);
merge_sort(A,m,y);
while(l///两个序列只要有一个非空
{
if(r>=y||(l//r>=y表示第二个序列为空时,后面的表示当两个都非空时,左边序列的元素小于右边的,满足这两个条件才复制左边的
T[i++]=A[l++];
else////否则复制右边的
{
T[i++]=A[r++];
cnt+=(m-l);
}
}
for(int i=x; i///把辅助数组中的元素复制回数组A
A[i]=T[i];
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
cnt=0;
for(int i=0; iscanf("%d",&a[i]);
b[i]=a[i];
}
merge_sort(a,0,n);
int MIN=cnt;
for(int i=0; i1-b[i]-b[i]);
MIN=min(MIN,cnt);
}
printf("%d\n",MIN);
}
return 0;
}