关于3倍数的逆序数,即当i<j时,a[i]>3a[j]的序列逆序数。
来源:互联网 发布:阿里云平台介绍 编辑:程序博客网 时间:2024/06/05 00:41
题目描述:
Recall the problem of finding the number of inversions. As in the course, we are given a
sequence of n numbers a 1,· · · , an, which we assume are all distinct, and we difine an inversion
to be a pair i < j such that a i > aj
We motivated the problem of counting inversions as a good measure of how different two
orderings are. However, one might feel that this measure is too sensitive. Let’s call a pair
a significant inversionif i < j and a i >3aj. Given anO(nlog n) algorithm to count the
number of significant inversions between two orderings.
解题思路:
根据本题的叙述,我们可以按照一般求逆序数的思路来解决这个问题。除了在计算逆序数加一的时候加入a[i]>3a[j]进行判断即可。根据定义,
我们这样定义一个序列的逆序数:序列a[1],a[2],a[3],a[4],...,a[n]。这个序列的逆序数C,等于a[1],a[2],a[3],a[4],...的逆序数的和。
即C=sum(Ci),其中我们定义Ci为满足a[i]>3*a[j](i<j)的数的总的个数,即Ci=sum(a[i]>3*a[j]),(i<j)。
下面,我们采用递归分治的算法进行伪代码说明解释:
INVERSION(a_array,start_position,end_position)
{
n=a_array.length;
If n==1
return 0;
else
{
mid_position=(start_position+end_position)/2;
N1=INVERSION(a_array,start_position,mid_position);
N2=INVERSION(a_array,mid_position+1,end_position);
N3=Merger(a_array,start_position,end_position);
}
return N=N1+N2+N3;
}
Merge (a_array,start_position,end_position)
{
得到数组a_array分开后的左右数组L_array,R_array;
i=0;j=0;
InversionCount=0;
for(k=0 to end)
{
if (L_array[i]>R_array[j])
{
A[k]=L_array[i];
i++;
}
if(L_array[i]>3*R_array[j])
InversionCount=InversionCount+j-k;
else
{
A[k]=R[j];
j++;
}
}
}
时间复杂性分析:
因为在这里,T(n)<2T(n/2)+cn,其中Merge的时间复杂度为cn,每一次的递归,规模减半,因此最终的时间复杂度为o(n log n).
- 关于3倍数的逆序数,即当i<j时,a[i]>3a[j]的序列逆序数。
- 三元逆序对 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组Codeforces 61E Enemy is weak
- 给定有序序列 a,b,求出所有a[i]+b[j]中所的第k小的数
- 给出一列数a1,a2,a3....an,求它们的逆序对数,即有多少个有序对(i,j) 使得i<j,ai>aj,n高达10的6次方
- 找出数组a[]中符合a[i]+a[j]=K的数对
- 关于Lintcode734形式为a^i b^j c^k的子序列数量
- 【分治法】在数组A中,返回i<j,且A[i]>A[j]的(i,j)配对的数量
- 数组a[i++]=j 与 a[i]++ 的区别
- CareerCup Find 3 indexes i,j,k such that, i<j<k and a[i] < a[j] < a[k]
- 一个数组a[0...n-1],求a[i]-a[j]的最大值,其中i>j
- 一个数组a[0...n-1],求a[j]-a[i]的最大值,其中i<j
- 找数组的i,j(j>i)使得a[j] - a[i]的值最大(算法)
- 求gcd(i,j,k)=m的方案数,i,j,k不同且属于1-n(组合数学+筛法(倍数法))
- a[i][j]与a[j][i]性能差别的原因
- 求a[j]-a[i]的最大值,其中j>=i
- 关于二维数组a[i][j]
- 运算优先级的坑a[i++] = a[j]
- 求一个数组(a(i,j))中元素相减的最大值,且i<=j
- 模仿企业团队合作开发——svn的简单搭建与使用(Windows)
- 乐观锁和悲观锁
- 浅析ReactNative之通信机制(一)
- 判断字符串中是否含有字母数字下划线
- Jetty 的安全性
- 关于3倍数的逆序数,即当i<j时,a[i]>3a[j]的序列逆序数。
- python使用汇总
- 腾讯云服务器linux系统下apache php mysql安装
- 协议号大全
- seitch中的default
- 表(List)的实现——算数学习(二)
- wxPython 进行GUI 设计解决PyNoAppError: The wx.App object must be created first!
- C++很简单的一种判断文件是否存在的办法
- XML学习