📌  相关文章
📜  执行给定操作后数组元素的最小可能总和

📅  最后修改于: 2021-05-19 19:00:21             🧑  作者: Mango

给定大小为N的数组arr []和数字X。如果数组的任何子数组(可能为空)arr [i],arr [i + 1],…都可以替换为arr [i] / x,arr [i + 1] / x,…。任务是找到可以获取的数组的最小和。
注意:给定的操作只能执行一次。
例子:

天真的方法:天真的方法是将所有可能的正子数组替换为通过将其除以X并计算总和而得到的值。但是任何给定数组的子数组总数为(N *(N + 1))/ 2 ,其中N是数组的大小。因此,该算法的运行时间为O(N 2 )
高效的方法:此问题可以在O(N)时间内有效解决。仔细观察,可以推断出,如果要替换的子数组满足以下条件,则总和可以为最小值:

  1. 子数组应为正数。
  2. 子数组的总和应为最大值。

因此,通过减少满足上述条件的子阵列,可以使总和最小。通过使用Kadane算法的细微变化,可以找到满足上述条件的子阵列。
在找到子数组的最大和之后,可以将其从数组的和中减去以获得最小和。由于子数组的最大和被替换为最大和/ X,因此可以将此因子添加到找到的和中。那是:

Minimum sum = (sumOfArr - subSum) + (subSum/ X)
where sumOfArr is the sum of all elements of the array
and subSum is the maximum sum of the subarray.

下面是上述方法的实现:

CPP
// C++ program to find the minimum possible sum
// of the array elements after performing
// the given operation
#include 
using namespace std;
 
// Function to find the maximum sum
// of the sub array
double maxSubArraySum(double a[], int size)
{
 
    // max_so_far represents the maximum sum
    // found till now and max_ending_here
    // represents the maximum sum ending at
    // a specific index
    double max_so_far = INT_MIN,
           max_ending_here = 0;
 
    // Iterating through the array to find
    // the maximum sum of the subarray
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        // If the maximum sum ending at a
        // specific index becomes less than 0,
        // then making it equal to 0.
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
 
// Function to find the minimum possible sum
// of the array elements after performing
// the given operation
double minPossibleSum(double a[], int n, double x)
{
    double mxSum = maxSubArraySum(a, n);
    double sum = 0;
 
    // Finding the sum of the array
    for (int i = 0; i < n; i++) {
        sum += a[i];
    }
 
    // Computing the minimum sum of the array
    sum = sum - mxSum + mxSum / x;
 
    cout << setprecision(2) << sum << endl;
}
 
// Driver code
int main()
{
    int N = 3;
    double X = 2;
    double A[N] = { 1, -2, 3 };
    minPossibleSum(A, N, X);
}


Java
// Java program to find the minimum possible sum
// of the array elements after performing
// the given operation
class GFG{
  
// Function to find the maximum sum
// of the sub array
static double maxSubArraySum(double a[], int size)
{
  
    // max_so_far represents the maximum sum
    // found till now and max_ending_here
    // represents the maximum sum ending at
    // a specific index
    double max_so_far = Integer.MIN_VALUE,
           max_ending_here = 0;
  
    // Iterating through the array to find
    // the maximum sum of the subarray
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
  
        // If the maximum sum ending at a
        // specific index becomes less than 0,
        // then making it equal to 0.
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
  
// Function to find the minimum possible sum
// of the array elements after performing
// the given operation
static void minPossibleSum(double a[], int n, double x)
{
    double mxSum = maxSubArraySum(a, n);
    double sum = 0;
  
    // Finding the sum of the array
    for (int i = 0; i < n; i++) {
        sum += a[i];
    }
  
    // Computing the minimum sum of the array
    sum = sum - mxSum + mxSum / x;
  
    System.out.print(sum +"\n");
}
  
// Driver code
public static void main(String[] args)
{
    int N = 3;
    double X = 2;
    double A[] = { 1, -2, 3 };
    minPossibleSum(A, N, X);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to find the minimum possible sum
# of the array elements after performing
# the given operation
 
# Function to find the maximum sum
# of the sub array
def maxSubArraySum(a, size):
 
    # max_so_far represents the maximum sum
    # found till now and max_ending_here
    # represents the maximum sum ending at
    # a specific index
    max_so_far = -10**9
    max_ending_here = 0
 
    # Iterating through the array to find
    # the maximum sum of the subarray
    for i in range(size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
 
        # If the maximum sum ending at a
        # specific index becomes less than 0,
        # then making it equal to 0.
        if (max_ending_here < 0):
            max_ending_here = 0
    return max_so_far
 
# Function to find the minimum possible sum
# of the array elements after performing
# the given operation
def minPossibleSum(a,n, x):
    mxSum = maxSubArraySum(a, n)
    sum = 0
 
    # Finding the sum of the array
    for i in range(n):
        sum += a[i]
 
    # Computing the minimum sum of the array
    sum = sum - mxSum + mxSum / x
    print(round(sum,2))
 
# Driver code
if __name__ == '__main__':
    N = 3
    X = 2
    A=[1, -2, 3]
    minPossibleSum(A, N, X)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the minimum possible sum
// of the array elements after performing
// the given operation
using System;
 
class GFG{
   
// Function to find the maximum sum
// of the sub array
static double maxSubArraySum(double[] a, int size)
{
   
    // max_so_far represents the maximum sum
    // found till now and max_ending_here
    // represents the maximum sum ending at
    // a specific index
    double max_so_far = Int32.MinValue,
           max_ending_here = 0;
   
    // Iterating through the array to find
    // the maximum sum of the subarray
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
   
        // If the maximum sum ending at a
        // specific index becomes less than 0,
        // then making it equal to 0.
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
   
// Function to find the minimum possible sum
// of the array elements after performing
// the given operation
static void minPossibleSum(double[] a, int n, double x)
{
    double mxSum = maxSubArraySum(a, n);
    double sum = 0;
   
    // Finding the sum of the array
    for (int i = 0; i < n; i++) {
        sum += a[i];
    }
   
    // Computing the minimum sum of the array
    sum = sum - mxSum + mxSum / x;
   
    Console.Write(sum +"\n");
}
   
// Driver code
public static void Main()
{
    int N = 3;
    double X = 2;
    double[] A = { 1, -2, 3 };
    minPossibleSum(A, N, X);
}
}
 
// This code is contributed by chitranayal


Javascript


输出:
0.5

时间复杂度: O(N)