HDU-1369-Minimum Inversion Number(树状数组|线段树,求逆序数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

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:  1166 1698 1540 1542 1255 

题目大意:给一个n个数的序列:a1,a2,a3,a4,a5.....an;

让他们进行交换成:

a2,a3,a4...an,a1;

a3,a4,a5.......an,a1,a2

.......

所有的数ai都要在0~n-1之间(很重要,递推式的条件!!!)

n种序列,找出其中最小的那个序列的逆序数的对数;

这道题写的时候是放在线段树里面的,但是用树状数组求逆序数似乎更简单一点,

找到规律,

先求出第一组序列的逆序数,将第一个元素放到后面,相当于

原来的序列减少一个a[i]个逆序数,在后面加上n-a[i]-1(减去它本身)个逆序数

处理出一组逆序数,然后递推找最小的,

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
int tree[5010];
int arr[5010];
int n;

int lowbit(int i)
{
	return i&(-i);
}

void updata(int i)
{
	while(i<=n)
	{
		tree[i]=tree[i]+1;
		i=i+lowbit(i);
	}
}

ll Query(int i)
{
	ll res=0;
	while(i>0)
	{
		res=res+tree[i];
		i=i-lowbit(i);
	}
	return res;
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		clean(arr,0);
		clean(tree,0);
		ll ans=0;
		for(ll i=1;i<=n;++i)
		{
			scanf("%d",&arr[i]);
			updata(arr[i]+1);
			ans=ans+i-Query(arr[i]+1);//逆序数的数量 
			//cout<<arr[i]<<" ";
		}
		ll res=ans;
		for(ll i=1;i<=n;++i)
		{
			ans=ans+(n-arr[i]-1)-arr[i];
			if(res>ans)
				res=ans;
		}
		
		cout<<res<<endl;	
	}

}

 

//树状数组很简单,但毕竟是放在线段树里面的,就用线段树再写一份把:

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
int tree[5010<<2];//一个树的空间 
int arr[5010];

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

int Query(int L,int R,int l,int r,int rt)
{
	//cout<<rt<<" "<<tree[rt]<<endl;
	if(L<=l&&r<=R)
		return tree[rt];
	int mid=(r+l)>>1;
	int res=0;
	if(L<=mid)
		res=res+Query(L,R,l,mid,rt<<1);
	if(R>mid)
		res=res+Query(L,R,mid+1,r,rt<<1|1);
	return res;
}

//void build_tree(int l,int r,int rt)
//{
//	if(l==r)
//	{
//		cout<<tree[rt]<<" ";
//		return ;
//	}
//	int mid=(r+l)>>1;
//	build_tree(l,mid,rt<<1);
//	build_tree(mid+1,r,rt<<1|1);
//	
//}

int main()
{
	int n;
	while(cin>>n)
	{
		clean(tree,0);
		clean(arr,0);
		int ans=0;
		for(int i=1;i<=n;++i)
		{
			cin>>arr[i];
			updata(arr[i]+1,1,n,1);//从根节点找,+1避免出现0 
			ans=ans+Query(arr[i]+2,n,1,n,1);
		}
		//build_tree(1,n,1);//调试 
		int res=ans;
		for(int i=1;i<=n;++i)
		{
			//cout<<ans<<" ";
			ans=ans+(n-2*arr[i]-1);
			if(res>ans)
				res=ans;
		}
		cout<<res<<endl;
	}
	
}

 

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务