-
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
-
DCD
DCD差分格式求解激波在刚性楔面上反射问题(DCD difference scheme for solving the shock wave in a rigid wedge surface reflection)
- 2012-08-22 14:16:03下载
- 积分:1
-
p2mesh
划分网格一个开源的库,有限元的算法,很好用(Meshing an open source library = for finite element)
- 2013-07-22 15:34:38下载
- 积分:1
-
最速下降法
最优化课程中的最速下降法matlab程序,利用了wolfe搜索和armijo线搜索(Matlab program of steepest descent method in optimization course, using Wolfe search and Armijo line search)
- 2021-03-26 18:19:13下载
- 积分:1
-
共轭梯度法
说明: 共轭梯度法(Conjugate Gradient)是介于最速下降法与牛顿法之间的一个方法,它仅需利用一阶导数信息,但克服了最速下降法收敛慢的缺点,又避免了牛顿法需要存储和计算Hesse矩阵并求逆的缺点,共轭梯度法不仅是解决大型线性方程组最有用的方法之一,也是解大型非线性最优化最有效的算法之一。 在各种优化算法中,共轭梯度法是非常重要的一种。其优点是所需存储量小,具有步收敛性,稳定性高,而且不需要任何外来参数。(Conjugate gradient method Gradient) is a method between the steepest descent method and Newton's method. It only uses the first derivative information, but overcomes the disadvantage of slow convergence of steepest descent method, and avoids the disadvantage of storing and calculating Hesse matrix and solving inverse of Newton's method. Conjugate gradient method is not only one of the most useful methods to solve large-scale linear equations, but also the most effective method to solve large-scale nonlinear optimization One of the algorithms of. Among all kinds of optimization algorithms, conjugate gradient method is very important. It has the advantages of small storage, step convergence, high stability and no external parameters.)
- 2020-06-27 15:46:08下载
- 积分:1
-
for90
薄膜生长蒙特卡洛方法的,适合各种原子的啊啊啊啊啊啊啊(The Monte Carlo method of film growth is suitable for various atoms)
- 2020-08-30 16:18:10下载
- 积分:1
-
WENO
weno格式解决一维黎曼问题的代码,fortran90编写(Fortran code for Riemann problem solver in weno scheme)
- 2021-03-21 10:49:17下载
- 积分:1
-
chaos-LargestLyapunov
给出一个求解连续混沌系统的最大Lyapunov指数的程序,经验证有很好的吻和度,并能应用的高维(超混沌)或其他混沌系统。(Solving a continuous chaotic system is given the maximum Lyapunov index procedure, proven good kiss and degrees, and can use the high-dimensional (super-chaotic), or other chaotic systems.)
- 2020-10-24 22:30:01下载
- 积分:1
-
udf_dpm_tongji
fluent多相流DPM模型中使用用户自定义函数对颗粒进行统计(DPM fluent multiphase flow model using user-defined functions for particle statistics)
- 2020-12-06 21:09:21下载
- 积分:1
-
leifing
用谱方法计算流体力学一些流动现象的整体稳定性,利用自然梯度算法,包括四元数的各种计算。( Spectral methods of computational fluid dynamics flow of some of the overall stability of the phenomenon, Use of natural gradient algorithm, Including quaternion various calculations.)
- 2017-01-03 17:25:25下载
- 积分:1