HDU1394 Minimum Inversion Number【线段树 点修改+区间查询】

Minimum Inversion Number

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

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


 

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个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2 a3 a4...an a1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列an a1 a2...an-1为止。计算上述所有排列的逆序数,输出最小逆序数。

思路

为了方便处理,我们统一将输入的数加一,这样数的范围就是[1,n]。设ans表示初始数列的逆序数,a[i]表示当前数列的第一个数,那么将a[i]移动到最后形成的新的数列的逆序数为多少?是不是ans2=ans-(a[i]-1)+(n-a[i])=ans+n+1-2*a[i],很容易想到,就是在原来的逆序数的基础上进行加减,减a[i]-1是因为把a[i]移动到最后了,原本和a[i]构成逆序对的(x,a[i]),x<a[i],x共a[i]-1个,变成了顺序对了,所以要减(a[i]-1),同理,原本和a[i]构成顺序对的(x,a[i]),x>a[i],x共n-a[i]个,变成了逆序对了,所以要加(n-a[i])。

那么现在问题的关键就是求出初始数列的逆序数,此处使用线段树进行求解,设结点rt表示的区间是[l,r],那么结点rt存放的信息为[l,r]范围内的数出现的次数,我们可以这样求初始数列的逆序数,求出每个数前面比它大的数的数量,具体看代码

C++代码

 

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

const int N=5010;

int a[N],sum[N<<2];

void pushup(int rt)
{
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
} 

//单点修改  sum[L]+=1
void update(int L,int l,int r,int rt)
{
	if(l==r)
	{
		sum[rt]+=1;
		return;
	}
	int m=(l+r)>>1;
	if(L<=m)
	  update(L,ls);
	else
	  update(L,rs);
	pushup(rt);
}

//区间查询,查询当前线段树中值的范围在[L,R]中的数的数量 
int query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	  return sum[rt];
	int m=(l+r)>>1;
	int res=0;
	if(L<=m)
	  res+=query(L,R,ls);
	if(R>m)
	  res+=query(L,R,rs);
	return res;
} 

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		//由于建树是让sum[i]=0,因此直接将sum数组置0就能代替建树过程 
		memset(sum,0,sizeof(sum));
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			a[i]++;//让a[i]加一,使得a[i]在[1,n]的范围而不是原来的[0,n-1],这样更好处理
			//查询a[i]的前面有多少数比a[i]大,即查询多又是个元素在[a[i]+1,n]的范围内 
			if(a[i]<n) ans+=query(a[i]+1,n,1,n,1);
			update(a[i],1,n,1);
		}
		//ans就是初始数列的逆序数,现在求变化n-1次的过程中的逆序数,求最少数
		for(int i=1,tmp=ans;i<n;i++)
		{
			tmp=tmp+n+1-2*a[i];//通过前一个状态下的逆序数求现在这个状态的逆序数 
			ans=min(ans,tmp);//取最少值 
		}
		printf("%d\n",ans);
	}
	return 0; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值