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

171 篇文章 0 订阅
8 篇文章 0 订阅


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
 

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;
 } 




拓展一下,如果要求正序数怎么办?很简单,无非是大小调一下
再问,如果要求满足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 )




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值