-
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
-
PWM
基于串口通信输出pwm方波用于控制电机转动速度(Output PWM square wave based on serial communication for controlling motor rotation speed)
- 2020-06-18 14:00:02下载
- 积分:1
-
Xu-White-model
用C++编写的xu-white模型,适合砂泥岩地层岩石物理建模,很实用(Written by C++ xu-white model, suitable for sand-shale formation rock physics modeling, it is practical)
- 2020-12-25 09:59:04下载
- 积分:1
-
CPPApplication-Programming-200
C++应用程序200例 让你在实践中学会和理解C++编程(C++ application 200 cases in practice, you learn and understand C++ programming)
- 2011-04-20 10:22:53下载
- 积分:1
-
0721689
影像匹配的源代码 Image matching source code()
- 2018-02-16 18:10:39下载
- 积分:1
-
基于k60平台的pwm例程
FTM的PWM功能;
改变PWM的占空比;
初始化FTM的正交解码功能 ;
FTM的输入捕捉功能 ;
FTM的中断功能;
- 2022-04-24 03:38:27下载
- 积分:1
-
rentdvd
数据库程序,是一个DVD出租系统的源码!
能记录删除出租记录和录像带的信息(database program, is a DVD rental system source code! Records can be deleted video rental records and information)
- 2007-03-31 18:06:18下载
- 积分:1
-
Modern-Periodic-Table-Project-in-C
Modern Periodic Table Project in C.
The Modern Periodic Table project in C is a simple console application built without the use of graphics. It is developed using the C programming language for the purpose of storing name, symbol, atomic number, atomic weight, and some important properties as well as to display them as per requirement of the user. The source code for this project is complete and totally error free, and we warmly welcome any feedbacks and suggestions.
- 2014-12-03 16:24:18下载
- 积分:1
-
哈夫曼树
利用哈夫曼树对简单文件进行压缩和解压缩,对数据的压缩集成(Compression and Decompression of Simple Files Using Huffman Tree)
- 2019-05-25 09:42:38下载
- 积分:1
-
Linux下生产者消费者模型
运用pthread线程库互斥锁机制和POSI
的信号量机制,找不到源码链接了,欢迎提供。
- 2022-04-19 22:10:53下载
- 积分:1