给定数组arr [] ,任务是将给定数组精确地附加到末尾K – 1次,并打印结果数组中的求反总数。
例子:
Input: arr[]= {2, 1, 3}, K = 3
Output: 12
Explanation:
Appending 2 copies of array arr[] modifes arr[] to {2, 1, 3, 2, 1, 3, 2, 1, 3}
The pairs (arr[i], arr[j]), where i < j and arr[i] > arr[j] are (2, 1), (2, 1), (2, 1), (3, 2), (3, 1), (3, 2), (3, 1), (2, 1), (2, 1), (3, 2), (3, 1), (2, 1)
Therefore, the total number of inversions are 12.
Input: arr[]= {6, 2}, K = 2
Output: 3
Explanation:
Appending 2 copies of array arr[] = {6, 2, 6, 2}
The pairs (arr[i], arr[j]), where i < j and arr[i] > arr[j] are (6, 2), (6, 2), (6, 2)
Therefore, the total number of inversions are 3.
天真的方法:最简单的方法是将给定数组的K个副本存储在一个向量中,然后找到所得向量的求逆数。
时间复杂度: O(N 2 )
辅助空间: O(K * N)
高效的方法:解决此问题的想法是首先找到给定数组(例如inv)中的求反总数。然后,在一个副本中计算成对的不同元素,例如X。现在,在通过等式附加数组的K个副本之后,计算反转的总数:
(inv*K + ((K*(K-1))/2)*X).
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// inversions in K copies of given array
void totalInversions(int arr[],
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Generate each pair
for (int j = 0; j < N; j++) {
// Check for each pair, if the
// condition is satisfied or not
if (arr[i] > arr[j] and i < j)
inv++;
// If pairs consist of
// distinct elements
if (arr[i] > arr[j])
X++;
}
}
// Count inversiosn in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
cout << totalInv << endl;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
totalInversions(arr, K, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count the number of
// inversions in K copies of given array
static void totalInversions(int arr[],
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
int i, j;
// Traverse the array
for (i = 0; i < N; i++)
{
// Generate each pair
for (j = 0; j < N; j++)
{
// Check for each pair, if the
// condition is satisfied or not
if(arr[i] > arr[j] && i < j)
inv++;
// If pairs consist of
// distinct elements
if(arr[i] > arr[j])
X++;
}
}
// Count inversiosn in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
System.out.println(totalInv);
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = arr.length;
totalInversions(arr, K, N);
}
}
// This code is contributed by bgangwar59.
Python3
# Python program of the above approach
# Function to count the number of
# inversions in K copies of given array
def totalInversions(arr, K, N) :
# Stores count of inversions
# in the given array
inv = 0
# Stores the count of pairs
# of distinct array elements
X = 0
# Traverse the array
for i in range(N):
# Generate each pair
for j in range(N):
# Check for each pair, if the
# condition is satisfied or not
if (arr[i] > arr[j] and i < j) :
inv += 1
# If pairs consist of
# distinct elements
if (arr[i] > arr[j]) :
X += 1
# Count inversiosn in the sequence
totalInv = X * K * (K - 1) // 2 + inv * K
# Prthe answer
print(totalInv)
# Driver Code
# Given array
arr = [ 2, 1, 3 ]
# Given K
K = 3
# Size of the array
N = len(arr)
totalInversions(arr, K, N)
# This code is contributed by susmitakundugoaldanga
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to count the number of
// inversions in K copies of given array
static void totalInversions(int []arr,
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
int i, j;
// Traverse the array
for (i = 0; i < N; i++)
{
// Generate each pair
for (j = 0; j < N; j++)
{
// Check for each pair, if the
// condition is satisfied or not
if(arr[i] > arr[j] && i < j)
inv++;
// If pairs consist of
// distinct elements
if(arr[i] > arr[j])
X++;
}
}
// Count inversiosn in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
Console.WriteLine(totalInv);
}
// Driver Code
public static void Main()
{
// Given array
int []arr = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = arr.Length;
totalInversions(arr, K, N);
}
}
// This code is contributed by jana_sayantan.
Javascript
12
时间复杂度: O(N 2 )
辅助空间: O(1)