Minimum Inversion Number(树状数组求逆序数+找数学规律)


Link:http://acm.hdu.edu.cn/showproblem.php?pid=1394


Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12203    Accepted Submission(s): 7446


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
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1698  1540  1542  1255  2795 
 

AC  code:
#include
#include
#include
#include
#include
#include
#define MAXN 5010
using namespace std;
int c[MAXN],n,num[MAXN];
int lowbit(int p)
{
	return p&(-p);
}
void up(int p,int v)
{
	while(p<=n)
	{
		c[p]+=v;
		p+=lowbit(p);
	}
}
int sum(int p)
{
	int s=0;
	while(p>0)
	{
		s+=c[p];
		p-=lowbit(p);
	}
	return s;
}
int main()
{
	int t,ans;
	while(~scanf("%d",&n))
	{
		ans=0;
		memset(c,0,sizeof(c));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			up(num[i]+1,1);//注意加1!!!并且必须先更新再求和
			ans+=i-sum(num[i]+1); 
		}
		int tmp=ans;
		for(int i=1;i<=n-1;i++)
		{
			tmp=tmp-num[i]+(n-1-num[i]);
			ans=min(tmp,ans);
		}
		printf("%d\n",ans);
	}
	return 0;
}



附上第二种做法(用线段树):


#include
#include
#include
#include
#include
#include
#define MAXN 5001
using namespace std;
struct node{
	int l,r,sum;
}tree[MAXN*4];
int ans,n;
int num[MAXN];
void build(int rt,int s,int e)
{
	tree[rt].l=s;
	tree[rt].r=e;
	if(s==e)
	{
		tree[rt].sum=0;
		return;
	}
	int mid=(s+e)/2;
	build(rt*2,s,mid);
	build(rt*2+1,mid+1,e);
	tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;
}
void update(int rt,int pos,int val)
{
	if(tree[rt].l==pos&&tree[rt].r==pos)
	{
		tree[rt].sum+=val;
		return;
	}
	int mid=(tree[rt].l+tree[rt].r)/2;
	if(pos<=mid)
	{
		update(rt*2,pos,val);
	}
	else
	{
		update(rt*2+1,pos,val);
	}
	tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;
}
void query(int rt,int ql,int qr)
{
	int mid=(tree[rt].l+tree[rt].r)/2;
	if(tree[rt].l==ql&&tree[rt].r==qr)
	{
		ans+=tree[rt].sum;
	}
	else if(qr<=mid)
	{
		query(rt*2,ql,qr);
	}
	else if(ql>mid)
	{
		query(rt*2+1,ql,qr);
	}
	else
	{
		query(rt*2,ql,mid);
		query(rt*2+1,mid+1,qr);
	}
}
int main()
{
	int i,ans1;
	while(~scanf("%d",&n))
	{
		build(1,0,n-1);
		ans1=0;
		for(i=0;i




以下拓展内容参考自 http://blog.csdn.net/wiking__acm/article/details/7920429:
拓展一下,如果要求正序数怎么办?很简单,无非是大小调一下
再问,如果要求满足ia[j]>a[k]的数对总数怎么办?

可以从中间的这个数入手,统计a[i]>a[j]的对数m,以及a[j]>a[k]的对数n,m*n就是。。。
要求a[i]>a[j]的个数还是一样的,那么a[j]>a[k]的个数呢?
两种思路:
1.得到a[i]>a[j]的对数后,将数列倒过来后再求a[j]
2.更简单的做法是,找到规律发现,n = 整个数列中比a[j]小的数 — 在a[j]前面已经出现的比a[j]小的数的个数
即(假设数列是从1开始的) n = (a[j] -1) - (j - 1 - m )




你可能感兴趣的:(求逆序数,HDU,ACM,树状数组,ACM,算法)