我们得到了一个偶数大小的数组,我们必须对数组进行排序,以使备用元素的乘积之和最小,而且我们必须找到该最小和。
例子:
Input : arr[] = {9, 2, 8, 4, 5, 7, 6, 0}
Output : Minimum sum of the product of
consecutive pair elements: 74
Sorted arr[] for minimum sum:
{9, 0, 8, 2, 7, 4, 6, 5}
Explanation : We get 74 using below
calculation in rearranged array.
9*0 + 8*2 + 7*4 + 6*5 = 74
Input : arr[] = {1, 2, 1, 4, 0, 5, 6, 0}
Output : Minimum sum of the product of
consecutive pair elements: 6
Sorted arr[] for minimum sum:
{6, 0, 5, 0, 4, 1, 2, 1}
Explanation : We get 6 using below:
6*0 + 5*0 + 4*1 + 2*1 = 6
这个问题是一个最小化两个数组的乘积之和的变体,允许排列。
为了使数组重新排列以使连续元素对的乘积之和最小,我们应使所有偶数索引元素递减,奇数索引元素按升序排列,所有n / 2个最大元素均作为偶数索引和接下来的n / 2个元素作为奇数索引,反之亦然。
现在,由于我们的想法很简单,我们应该对主数组进行排序,并分别分别创建两个辅助数组evenArr []和oddArr []。我们遍历输入数组,并将n / 2个最大元素放入evenArr []中,然后将n / 2个最大元素放入oddArr []中。然后,我们按降序对evenArr []和按升序对oddArr []进行排序。最后,逐个元素复制evenArr []和oddArr []以获取所需的结果,并应计算所需的最小和。
下面是上述方法的实现:
C++
// C++ program to sort an array such that
// sum of product of alternate element
// is minimum.
#include
using namespace std;
int minSum(int arr[], int n)
{
// create evenArr[] and oddArr[]
vector evenArr;
vector oddArr;
// sort main array in ascending order
sort(arr, arr+n );
// Put elements in oddArr[] and evenArr[]
// as per desired value.
for (int i = 0; i < n; i++)
{
if (i < n/2)
oddArr.push_back(arr[i]);
else
evenArr.push_back(arr[i]);
}
// sort evenArr[] in descending order
sort(evenArr.begin(), evenArr.end(), greater());
// merge both sub-array and
// calculate minimum sum of
// product of alternate elements
int i = 0, sum = 0;
for (int j=0; j
Java
// Java program to sort an array such that
// sum of product of alternate element
// is minimum.
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
class GFG {
static int minSum(int arr[], int n) {
// create evenArr[] and oddArr[]
Vector evenArr = new Vector<>();
Vector oddArr = new Vector<>();
// sort main array in ascending order
Arrays.sort(arr);
// Put elements in oddArr[] and evenArr[]
// as per desired value.
for (int i = 0; i < n; i++) {
if (i < n / 2) {
oddArr.add(arr[i]);
} else {
evenArr.add(arr[i]);
}
}
// sort evenArr[] in descending order
Comparator comparator = Collections.reverseOrder();
Collections.sort(evenArr,comparator);
// merge both sub-array and
// calculate minimum sum of
// product of alternate elements
int i = 0, sum = 0;
for (int j = 0; j < evenArr.size(); j++) {
arr[i++] = evenArr.get(j);
arr[i++] = oddArr.get(j);
sum += evenArr.get(j) * oddArr.get(j);
}
return sum;
}
// Driver program
public static void main(String[] args) {
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = arr.length;
System.out.println("Minimum required sum = "
+ minSum(arr, n));
System.out.println("Sorted array in required format : ");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Python3
# Python 3 program to sort an array such
# that sum of product of alternate element
# is minimum.
def minSum(arr, n):
# create evenArr[] and oddArr[]
evenArr = []
oddArr = []
# sort main array in ascending order
arr.sort()
# Put elements in oddArr[] and
# evenArr[] as per desired value.
for i in range(n):
if (i < n // 2):
oddArr.append(arr[i])
else:
evenArr.append(arr[i])
# sort evenArr[] in descending order
evenArr.sort(reverse = True)
# merge both sub-array and
# calculate minimum sum of
# product of alternate elements
i = 0
sum = 0
for j in range(len(evenArr)):
arr[i] = evenArr[j]
i += 1
arr[i] = oddArr[j]
i += 1
sum += evenArr[j] * oddArr[j]
return sum
# Driver Code
if __name__ == "__main__":
arr = [ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 ]
n = len(arr)
print( "Minimum required sum =",
minSum(arr, n))
print("Sorted array in required format : ",
end = "")
for i in range(n):
print(arr[i], end = " ")
# This code is contributed by ita_c
C#
// Program to sort an array such that
// sum of product of alternate element
// is minimum.
using System;
using System.Collections.Generic;
class GFG
{
static int minSum(int []arr, int n)
{
// create evenArr[] and oddArr[]
List evenArr = new List();
List oddArr = new List();
int i;
// sort main array in ascending order
Array.Sort(arr);
// Put elements in oddArr[] and
// evenArr[] as per desired value.
for (i = 0; i < n; i++)
{
if (i < n / 2)
{
oddArr.Add(arr[i]);
}
else
{
evenArr.Add(arr[i]);
}
}
// sort evenArr[] in descending order
evenArr.Sort();
evenArr.Reverse();
// merge both sub-array and
// calculate minimum sum of
// product of alternate elements
int k = 0, sum = 0;
for (int j = 0; j < evenArr.Count; j++)
{
arr[k++] = evenArr[j];
arr[k++] = oddArr[j];
sum += evenArr[j] * oddArr[j];
}
return sum;
}
// Driver Code
public static void Main()
{
int []arr = {1, 5, 8, 9, 6,
7, 3, 4, 2, 0};
int n = arr.Length;
Console.WriteLine("Minimum required sum = " +
minSum(arr, n));
Console.WriteLine("Sorted array in " +
"required format : ");
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code contributed by 29AjayKumar
Javascript
输出:
Minimum required sum = 60
Sorted array in required format : 9 0 8 1 7 2 6 3 5 4