-
11087 统计逆序对
Description
设a[0…n-1]是一个包含n个数的数组,若在i<j的情况下,有a[i]>a[j],则称(i, j)为a数组的一个逆序对(inversion)。
比如 <2,3,8,6,1> 有5个逆序对。请采用类似“合并排序算法”的分治思路以O(nlogn)的效率来实现逆序对的统计。
一个n个元素序列的逆序对个数由三部分构成:
(1)它的左半部分逆序对的个数,(2)加上右半部分逆序对的个数,(3)再加上左半部分元素大于右半部分元素的数量。
其中前两部分(1)和(2)由递归来实现。要保证算法最后效率O(nlogn),第三部分(3)应该如何实现?
此题请勿采用O(n^2)的简单枚举算法来实现。
并思考如下问题:
(1)怎样的数组含有最多的逆序对?最多的又是多少个呢?
(2)插入排序的运行时间和数组中逆序对的个数有关系吗?什么关系?
输入格式
第一行:n,表示接下来要输入n个元素,n不超过10000。
第二行:n个元素序列。
输出格式
逆序对的个数。
输入样例
5
2 3 8 6 1
输出样例
5(Set a[0... N-1] is a n array containing n numbers. If there is a [i] > a [j] i n the case of I < j, then (i, j) is a n inversion pair of a array.
For example, <2,3,8,6,1> has five reverse pairs. Please use the idea of "merge sorting algorithm" to achieve the statistics of inverse pairs with O (nlogn) efficiency.
The number of inverse pairs of a sequence of n elements consists of three parts:
(1) The number of reverse pairs in the left half, (2) the number of reverse pairs in the right half, (3) the number of elements in the left half is greater than that in the right half.
The first two parts (1) and (2) are implemented by recursion. To ensure the final efficiency of the algorithm O (nlogn), how should the third part (3) be implemented?
Do not use O (n ^ 2) simple enumeration algorithm to solve this problem.)
- 2019-01-07 23:52:06下载
- 积分:1
-
8078349
用MATLAB实现模拟立方体的重构,是计算机视觉中三维重构的重要的应用实验(Simulating cube reconstruction with MATLAB is an important application experiment of 3D reconstruction in computer vision.)
- 2018-11-15 12:45:50下载
- 积分:1
-
fourier_trans
傅立叶变换的C语言实现,可在Turbo C以及Visual C++下实现。(Fourier Transform C language, in Turbo C and Visual C under.)
- 2006-12-31 11:06:45下载
- 积分:1
-
GGSA
改进的万有引力搜索算法的matlab程序,经过测试,100%可以使用的。(gravitational search algorithm matlab program, after testing, 100% can be used.)
- 2018-06-24 17:56:52下载
- 积分:1
-
非线性振动
说明: 非线性振动.pdf 刘延柱编写
恢复力与位移不成正比或阻尼力不与速度一次方成正比的系统的振动。尽管线性振动理论早已相当完善,在工程上也已取得广泛和卓有成效的应用,但在实际问题中,总有一些用线性理论无法解释的现象。一般说,线性模型只适用于小运动范围,起出这一范围,按线性问题处理就不仅在量上会引起较大误差,而且有时还会出现质上的差异,这就促使人们研究非线性振动。(Nonlinear Vibration. Compilation of PDF Liu Yanzhu)
- 2020-11-23 20:09:34下载
- 积分:1
-
蚁群算法优化参数
说明: 蚁群算法优化随机共振参数,用于滚动轴承故障诊断(Ant colony optimization stochastic resonance parameters, bearing fault diagnosis)
- 2021-04-06 13:29:02下载
- 积分:1
-
ch01
ANSYS教程,《ANSYS有限元分析实用教程》源代码("ANSYS finite element analysis of practical course," the source code)
- 2010-05-25 14:26:26下载
- 积分:1
-
edgeFEM2D
Edge element based 2D FEM code to calculate Electromagnetic Simulations
- 2008-02-09 05:07:14下载
- 积分:1
-
SOMP
同时正交匹配追踪算法的MATLAB程序,一些文献中也被翻译成同步正交匹配追踪。(Simultaneous Orthogonal Matching Pursuit(SOMP))
- 2020-12-02 20:59:25下载
- 积分:1
-
matinv
矩阵求逆,使用fortran语言编写,希望能对有需要的朋友有所帮助(Matrix inversion, written in FORTRAN language, hoping to help friends in need)
- 2020-12-11 17:49:18下载
- 积分:1