HDU1394-Minimum Inversion Number

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,表示数列中数的数量,这个数列是首尾相接的,所以要求最小的逆序数!唉!开始我居然没有看到这句话,害的我弄了好久,一直是22,得不到16,就怪英语太差(垃圾百度翻译)。

解析:
每插入一个数时,先查询从book[i]+1~n这个区间已经插入的数的数量,然后再插入该数,更新线段树。例如:
1 3 6 9 0 8 5 7 4 2
(1)当插入第一个数时,从2~n这个区间的数已经插入0个,然后记录1这个数的叶子节点自加1;
(2)然后插入第二个数,从4~n这个区间的数已经插入0个,然后记录3这个数的叶子节点自加1;
(3)然后插入第三个数,从7~n这个区间的数已经插入0个,然后记录6这个数的叶子节点自加1;
(4)然后插入第四个数,从10~n这个区间的数已经插入0个,然后记录9这个数的叶子节点自加1;
(5)然后插入第五个数,从1~n这个区间的数已经插入4个,然后记录0这个数的叶子节点自加1;
(6)然后插入第六个数,从9~n这个区间的数已经插入1个,然后记录8这个数的叶子节点自加1;
(7)然后插入第七个数,从6~n这个区间的数已经插入3个,然后记录5这个数的叶子节点自加1;
(8)然后插入第八个数,从8~n这个区间的数已经插入2个,然后记录7这个数的叶子节点自加1;
(9)然后插入第九个数,从5~n这个区间的数已经插入5个,然后记录4这个数的叶子节点自加1;
(10)然后插入第十个数,从3~n这个区间的数已经插入7个,然后记录2这个数的叶子节点自加1;
这样就可以求出给出数列的逆序数。

然后就是求依次变化后的数列的逆序数:
我们举个例子:(t1表示a[1]前比a[1]大的数,t2表示a[1]后比a[1]小的数)
3 2 5 4 6 1
t1=0 t2=3;
移动后数列变化为:
2 5 4 6 1 3
t1=0 t2=4;
那么你是否已经发现规律?对,t1总是0,t2总是n-a[1]
那么逆序数就可以变化为:
由于a[1]开始在第一个位置,它前面没有任何一个数,所以他所有比他小的数都在其后并组成逆序对,共a[1]-1个;在移至末尾后,这些逆序对就不再是逆序对了;同时,移至末尾后,他前面所有比他大的数都与他组成逆序对(因为原本在第一个位置时,前面没有任何一个数,所以更不存在比他大的数),所以要加上n-a[1]个,所以公式为:
sum+(n-a[1])-(a[1]-1)=sum+n-2*a[1]-1

代码:

#include<iostream>
#include<cstdio>
#include<algorithm> 
#define N 5005

using namespace std;

int book[N];
int tree[N<<2];

void build(int l,int r,int i)
{
	tree[i]=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(l,mid,i<<1);
	build(mid+1,r,i<<1|1);
}

void updata(int l,int r,int i,int x)
{
	if(l==r)
	{
		tree[i]++;
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid)
		updata(l,mid,i<<1,x);
	else
		updata(mid+1,r,i<<1|1,x);
	tree[i]=tree[i<<1]+tree[i<<1|1];
}

int query(int l,int r,int i,int lt,int rt)
{
	if(lt<=l&&r<=rt)
		return tree[i];
	int sum=0,mid=(l+r)>>1;
	if(lt<=mid)
		sum+=query(l,mid,i<<1,lt,rt);
	if(rt>mid)
		sum+=query(mid+1,r,i<<1|1,lt,rt);
	return sum;
}

int main()
{
	int n,sum;
	while(cin>>n)
	{
		sum=0;
		build(1,n,1);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&book[i]);
			sum+=query(1,n,1,book[i]+1,n);
			updata(1,n,1,book[i]+1);
		}
		int ans=sum;
		for(int i=1;i<=n;i++)
		{
			sum+=(n-2*book[i]-1);
			ans=min(ans,sum);
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值