您的位置:首页 > 其它

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

2015-02-27 22:49 218 查看
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<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#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<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#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<n;i++)
		{
			scanf("%d",&num[i]);
			ans=0;
			query(1,num[i],n-1);
			ans1+=ans;
	        update(0,num[i],1);
		}
		int t=ans1;
		for(int i=0;i<n-1;i++)
		{
			ans1=ans1-num[i]+(n-1-num[i]);
			t=min(t,ans1);
		}
	    printf("%d\n",t);
	}
	return 0;
 }


以下拓展内容参考自/article/2645059.html

拓展一下,如果要求正序数怎么办?很简单,无非是大小调一下

再问,如果要求满足i<j<k,且a[i]>a[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]<a[k]的对数

2.更简单的做法是,找到规律发现,n = 整个数列中比a[j]小的数 — 在a[j]前面已经出现的比a[j]小的数的个数

即(假设数列是从1开始的) n = (a[j] -1) - (j - 1 - m )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: