📌  相关文章
📜  最小化由相同索引元素的乘积组成的所有子阵列的总和

📅  最后修改于: 2021-05-13 23:44:05             🧑  作者: Mango

给定长度为N的两个数组arr []arr2 [] ,任务是在重新排列第二个数组后,找到由两个数组的相同索引元素的乘积组成的所有子数组的最小和

注意:由于答案可能非常大,请以10 9 + 7模输出答案。

例子:

方法:
可以观察到,i元素出现在(i + 1)*(n – i)数组中。因此,任务是使(i +1)*(n – i)* a [i] * b [i]之和最大。请按照以下步骤解决问题:

  • 由于arr2 []的元素只能重新排列,因此(i +1)*(n – i)* a [i]的值对于每个i元素都是恒定的。
  • 因此,为所有指标计算(i +1)*(n – i)* a [i]的值,然后乘积进行排序
  • 排序的阵列ARR2 []降序排列。
  • 对于每个i索引,以降序计算(i +1)*(n – i)* a [i]和升序为arr2 [i]乘积之和。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
#define ll long long
using namespace std;
 
const int mod = (int)1e9 + 7;
 
// Returns the greater of
// the two values
bool comp(ll a, ll b)
{
    if (a > b)
        return true;
    else
        return false;
}
 
// Function to rearrange the second array such
// that the sum of its product of same indexed
// elements from both the arrays is minimized
ll findMinValue(vector& a, vector& b)
{
 
    int n = a.size();
 
    // Stores (i - 1) * (n - i) * a[i]
    // for every i-th element
    vector pro(n);
 
    for (int i = 0; i < n; ++i) {
 
        // Updating the value of pro
        // according to the function
        pro[i] = ((ll)(i + 1) * (ll)(n - i));
        pro[i] *= (1LL * a[i]);
        ;
    }
 
    // Sort the array in revese order
    sort(b.begin(), b.end(), comp);
 
    // Sort the products
    sort(pro.begin(), pro.end());
 
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
 
        // Updating the ans
        ans += (pro[i] % mod * b[i]) % mod;
        ans %= mod;
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
int main()
{
 
    vector a = { 1, 2, 3 };
    vector b = { 2, 3, 2 };
    // Function call
    cout << findMinValue(a, b) << endl;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
static int mod = (int)1e9 + 7;
 
// Function to rearrange the second array such
// that the sum of its product of same indexed
// elements from both the arrays is minimized
static int findMinValue(int [] a, int []b)
{
    int n = a.length;
 
    // Stores (i - 1) * (n - i) * a[i]
    // for every i-th element
    int [] pro = new int[n];
 
    for(int i = 0; i < n; ++i)
    {
         
        // Updating the value of pro
        // according to the function
        pro[i] = ((i + 1) * (n - i));
        pro[i] *= (1L * a[i]);
        ;
    }
 
    // Sort the array in revese order
    Integer[] input = Arrays.stream(b).boxed(
                      ).toArray(Integer[]::new);
                       
    Arrays.sort(input, (x, y) -> y - x);
    b = Arrays.stream(input).mapToInt(
        Integer::intValue).toArray();
         
    // Sort the products
    Arrays.sort(pro);
 
    int ans = 0;
    for(int i = 0; i < n; ++i)
    {
         
        // Updating the ans
        ans += (pro[i] % mod * b[i]) % mod;
        ans %= mod;
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int []a = { 1, 2, 3 };
    int []b = { 2, 3, 2 };
     
    // Function call
    System.out.print(findMinValue(a, b) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 Program to implement
# the above approach
mod = 1e9 +7
 
# Function to rearrange
# the second array such
# that the sum of its
# product of same indexed
# elements from both
# the arrays is minimized
def findMinValue(a, b):
 
    n = len(a)
 
    # Stores (i - 1) * (n - i) * a[i]
    # for every i-th element
    pro = [0] * (n)
 
    for i in range (n):
 
        # Updating the value of pro
        # according to the function
        pro[i] = ((i + 1) * (n - i))
        pro[i] *= (a[i])
 
    # Sort the array in revese order
    b.sort(reverse = True)
 
    # Sort the products
    pro.sort()
 
    ans = 0
    for i in range (n):
 
        # Updating the ans
        ans += (pro[i] % mod * b[i]) % mod
        ans %= mod
    
    # Return the ans
    return ans
 
# Driver code
if __name__ == "__main__":
 
    a = [1, 2, 3]
    b = [2, 3, 2]
     
    # Function call
    print (int(findMinValue(a, b)))
 
# This code is contributed by Chitranayal


输出:
43


时间复杂度: O(N log N)
辅助空间: O(N)