给定一个由 N 个整数组成的数组arr[] 。任务是找到反演的次数。如果arr[i] > arr[j] 且 i < j ,则两个元素arr[i]和arr[j]形成反转。
例子
Input: arr[] = {8, 4, 2, 1}
Output: 6
Explanation:
Given array has six inversions:
- (8, 4): arr[0] > arr[1] and 0 < 1
- (8, 2): arr[0] > arr[2] and 0 < 2
- (8, 1): arr[0] > arr[3] and 0 < 3
- (4, 2): arr[1] > arr[2] and 1 < 2
- (4, 1): arr[1] > arr[3] and 1 < 3
- (2, 1): arr[2] > arr[3] and 2 < 3
Input: arr[] = {2, 3}
Output: 0
Explanation:
There is no such pair exists such that arr[i] > arr[j] and i < j.
我们已经讨论了以下方法:
- 计数数组中的反转|设置 1(使用归并排序)
- 计算数组中的反转 |组 2(使用自平衡 BST)
- 在 C++ STL 中使用 Set 计数反转
- 计算数组中的反转 |第 3 组(使用 BIT)
在这篇文章中,我们将讨论使用有序集和 GNU C++ PBDS 的方法。
方法:
我们将使用函数order_of_key(K) ,它返回在log N时间内严格小于 K 的元素数。
- 在 Ordered_Set 中插入数组的第一个元素。
- 对于arr[] 中的所有剩余元素,请执行以下操作:
- 在 Ordered_Set 中插入当前元素。
- 使用函数order_of_key(arr[i]+1)在 Ordered_Set 中查找严格小于当前元素 + 1的元素数。
- Ordered_Set 的大小和 order_of_key(current_element + 1) 之间的差异将给出当前元素的反转计数。
例如:
arr[] = {8, 4, 2, 1} Ordered_Set S = {8} For remaining element in arr[]: At index 1, the element is 4 S = {4, 8} key = order_of_key(5) = 1 The difference between size of S and key gives the total number of inversion count for that current element. inversion_count = S.size() - key = 2 - 1 = 1 Inversion Pairs are: (8, 4) At index 2, the element is 2 S = {2, 4, 8} key = order_of_key(3) = 1 inversion_count = S.size() - key = 3 - 1 = 2 Inversion Pairs are: (8, 2) and (4, 2) At index 3, the element is 1 S = {1, 2, 4, 8} key = order_of_key(2) = 1 inversion_count = S.size() - key = 4 - 1 = 3 Inversion Pairs are: (8, 1), (4, 1) and (2, 1) Total inversion count = 1 + 2 + 3 = 6
下面是上述方法的实现:
// Ordered set in GNU C++ based
// approach for inversion count
#include
#include
#include
using namespace __gnu_pbds;
using namespace std;
// Ordered Set Tree
typedef tree,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// Returns inversion count in
// arr[0..n-1]
int getInvCount(int arr[], int n)
{
int key;
// Intialise the ordered_set
ordered_set set1;
// Insert the first
// element in set
set1.insert(arr[0]);
// Intialise inversion
// count to zero
int invcount = 0;
// Finding the inversion
// count for current element
for (int i = 1; i < n; i++) {
set1.insert(arr[i]);
// Number of elements strictly
// less than arr[i]+1
key = set1.order_of_key(arr[i] + 1);
// Difference between set size
// and key will give the
// inversion count
invcount += set1.size() - key;
}
return invcount;
}
// Driver's Code
int main()
{
int arr[] = { 8, 4, 2, 1 };
int n = sizeof(arr) / sizeof(int);
// Function call to count
// inversion
cout << getInvCount(arr, n);
return 0;
}
输出:
6
时间复杂度: O(Nlog N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live