HDOJ 1394 Minimum Inversion Number(求逆序数—暴力or线段树or树状数组:单点更新,区间求和)

Minimum Inversion Number

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


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
 

题意:给出一个n个数的序列,序列中包含0,1,2,3,4 ....... n-1这些数(不一定按序给出)。先计算一下初始队列的逆序数。然后将第一个元素a1放到an后面去,形成一个新的序列,在求出它的逆序数。继续执行此操作,一共执行n-1次,直到数列变成an,an-1,....a2,a1为止。 计算所有的数列的逆序数,输出最小的逆序数。

题解:关于逆序数的概念:百度百科逆序数 

解法一:

暴力法:暴力解出初始序列的逆序数,时间复杂度为O(n^2)。然后更新逆序数,若此时开头的数字为a[i],那么将a[i]移到最后一位,逆序数会减少a[i],然后会增加n-a[i]-1个逆序数,所以num=num+n-1-2*a[i]。 复杂度为O(n)。

暴力法时间复杂度为:O(n^2)。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5010
int a[maxn];
int main()
{
	int n,i,j,cnt,num;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;++i)
		{
			scanf("%d",&a[i]);
		}
		num=0;
		for(i=0;i<n;++i)
		{
			cnt=0;
			for(j=i+1;j<n;++j)
			{
				if(a[j]<a[i])
					cnt++;
			}
			num+=cnt;
		}
		int ans=num;
		for(i=0;i<n-1;++i)
		{
			num=num+n-1-2*a[i];
			ans=min(ans,num);
		}
		printf("%d\n",ans);
	}
	return 0;
}


解法二:
线段树:这里使用线段树统计初始序列的逆序数。在树中每个叶子节点表示这个节点是否出现过。步入i代表的叶子节点是[1,1],那么sum[i]=1,表示这个数已经出现过了; j表示叶子节点是[2,7],sum[j]=4,表示在2到7这六个数中出现了4个。 
如果我们当前处理的数字是a[i],那么我们用query(a[i]+1,n-1,1,0,n-1)查询在[a[i]+1,n-1]中有多少个的数在a[i]之前出现了,即可以求出a[i]的逆序数,再更新a[i],将包含a[i]的区间都加一。
更新逆序数的方式还是利用公式num=num+n-1-2*a[i]。

线段树的解法时间复杂度为:O(n*logn)。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5010
#define lson i*2,l,m
#define rson i*2+1,m+1,r
int a[maxn],sum[maxn*4];

void PushUp(int x)
{
	sum[x]=sum[x*2]+sum[x*2+1];
}

void build(int i,int l,int r)
{
	if(l==r)
	{
		sum[i]=0;
		return ;
	}
	int m=(l+r)/2;
	build(lson);
	build(rson);
	PushUp(i);
}

int query(int ql,int qr,int i,int l,int r)
{
	if(ql<=l&&r<=qr)
		return sum[i];
	int m=(l+r)/2;
	int res=0;
	if(ql<=m)
		res+=query(ql,qr,lson);
	if(qr>m)
		res+=query(ql,qr,rson);
	return res; 
}

void update(int pos,int i,int l,int r)
{
	if(l==r)
	{
		sum[i]++;
		return ;
	}
	int m=(l+r)/2;
	if(pos<=m)
		update(pos,lson);
	else
		update(pos,rson);
	PushUp(i); 
}

int main()
{
	int n,i,cnt;
	while(scanf("%d",&n)!=EOF)
	{
		memset(sum,0,sizeof(sum));//这一步替代了build(1,0,n-1) 
		cnt=0;
		for(i=1;i<=n;++i)
		{
			scanf("%d",&a[i]);
			cnt+=query(a[i]+1,n-1,1,0,n-1);
			update(a[i],1,0,n-1);//更新包含a[i]的区间 
		}
		int ans=cnt;
		for(i=1;i<n;++i)
		{
			cnt=cnt+n-1-2*a[i];
			ans=min(cnt,ans);
		}
		printf("%d\n",ans);
	}
	return 0;
}


2016/3/17更新

树状数组解法:这里使用树状数组统计初始排列的逆序数个数。用sum(x)查询到的就是在x之前出现的数字中比x小的个数,所以用sum(n)-sum(x)就可以求得对于x之前出现的比x大的数的个数,即x的逆序数。 最后将x加入BIT中,更新比x大的数。    求出初始队列的逆序数就可以按照上面的方法循环出答案了,不过在树状数组中,为了避免0的影响,我将每位都加1了,最后的关系变成了 num+=(n+1-2*a[i])。 这个在纸上画画就知道了。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5010
int bit[maxn],n;
int a[maxn]; 

int sum(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=bit[x];
		x-=(x&-x);
	}
	return ans;
}

void add(int x)
{
	while(x<=n)
	{
		bit[x]+=1;
		x+=(x&-x);
	}
}

int main()
{
	int ans,num,i;
	while(scanf("%d",&n)!=EOF)
	{
		memset(bit,0,sizeof(bit));
		num=0;
		for(i=0;i<n;++i)
		{
			scanf("%d",&a[i]);
			a[i]++;
			num+=sum(n)-sum(a[i]);
			add(a[i]);
		}
		ans=0x3f3f3f;
		for(i=0;i<n;++i)
		{
			num+=(n+1-2*a[i]);
			ans=min(ans,num);
		}
		printf("%d\n",ans);
	}
	return 0;
} 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值